我使用的是ruby "2.0.0“和'rails','~> 4.0.0‘和’rspec‘,"2.14.2”(引用于Gemfile )。我有两个完全不同的应用程序,我在这两个程序上都有相同的错误。
我用脚手架发电机做控制器。所以它也产生了我的控制器测试。尤其是,更新操作立即中断。测试代码
describe "PUT update" do
describe "with valid params" do
it "updates the requested product" do
produc
在Rails控制器中使用RSpec测试索引操作和响应时,我得到了这个错误:JSON::ParserError: A JSON text must at least contain two octets!
最常见的修复方法--包括render_views --不起作用,并且没有传入nil。测试没有命中视图。当我在控制器的索引操作中插入render json: {test: 'hello world'}, status: 200 and return,并在视图(index.json.jbuilder)中和测试中的get :index之后插入一个窥探时,我可以看到有一个
在Rails中有这个模型:
class User < ActiveRecord::Base
def self.create_auth_from_hash(hash)
# stuff
end
end
我希望对create_from_auth_hash方法进行存根,以便隔离控制器行为。显然,当前的语法是:
expect_any_instance_of(User).to receive(:create_from_auth_hash).and_return(false)
但是,即使我没有发现错误,模型方法仍然被调用。
如何使用存根模型方法?我使用的是Rails 4.1和RSpec
在rails应用程序中测试我的一名员工时,我遇到了问题。看起来是这样的:
class UserStatisticsWorker
include Sidekiq::Worker
include Sidetiq::Schedulable
def perform(administration_id = nil)
administrations(administration_id).find_each do |administration|
User::StatisticsCalculator.new.recalculate_if_needed(administrat
如果我有一个模型 module MyModule
def bar(str)
puts str
end
end
MyModel < ActiveRecord::Base
include MyModule
bar('foo')
end 我的规格: describe MyModel do
before do
described_class.stubs(:bar)
end
it 'calls bar with correct arguments' do
# This does not work becaus
在我的Rails项目中,我使用的是使用any_instance的rspec-模拟,但我希望避免这种弃用消息:
Using any_instance from rspec-mocks' old :should syntax without explicitly enabling the syntax is deprecated. Use the new :expect syntax or explicitly enable :should instead.
这是我的规格:
describe (".create") do
it 'should return er
我在Rails 6中使用Rspec 3.8.0。
我有一个模型A,它是belongs_to B.,我试图用A作为subject编写一个单元测试。
expect(subject.b).to receive(:to_s)
subject.my_fn
然而,这个规范总是失败的,它说B的实例没有接收到消息,尽管我已经将binding.pry放在实际的代码中运行,并验证了a.b.to_s是否被调用:
class A
def my_fn
binding.pry
b.to_s
end
end
我甚至试过:
expect(a).to receive(:b).and_return(b)
下面这段代码按预期工作:
Object.any_instance.should_receive(:subscribe)
但是当使用新的rspec期望时,它不起作用:
expect(Object.any_instance).to receive(:subscribe)
错误是:
expected: 1 time with any arguments
received: 0 times with any arguments
我如何使用expect()来接收?
我有一套服务和子模块如下:
class Calculate::Document
def self.build
new
end
def initialize
@country_codes = [1,2,3]
end
def call(document)
...
@document = document
calculate_by_country
end
private
def calculate_by_country
#define by country
locatio
我们正在做一个使用RSpec 2的rails 3.2.1项目。我的问题是,我应该测试我的每个活动记录模型的基本持久性吗?在我的C# / NHibernate时代,我经常这样做,以确保有正确的表/映射。
因此,如果我有一个具有姓名、地址和电话字段的客户,我可能会像这样编写一个rspec:
describe Customer do
it "saves & retrieves its fields to and from the db"
c = Customer.new
c.name = "Bob Smith"
c.addre
我想测试在初始化类的新实例时是否调用了某个方法。在我的项目中,我有以下代码:
class Journey
def initialize(entry_station)
@journey_complete = false
@entry_station = entry_station
self.start_journey
end
def start_journey
...
end
end
我想编写一个RSpec测试,检查在初始化时所有新的旅行实例都调用了start_journey。通常,要检查方法是否已运行,我会expect一个对象来接收特定的消息,
我在rails中有一个导入控制器,可以将几个具有多个记录的csv文件导入到我的数据库中。我想在RSpec中测试记录是否真的是使用RSpec保存的:
<Model>.any_instance.should_receive(:save).at_least(:once)
然而,我得到的错误是:
The message 'save' was received by <model instance> but has already been received by <another model instance>
一个人为设计的控制器示例:
rows
我的控制器看起来是这样的:
def index
params[:page]||1
@stories = Story.all.page(params[:page]).per(5)
end
当我试图通过使用RSpec为链接行编写控制器测试时,我似乎无法通过测试。
我的controller_spec看起来像是:
describe '#index' do
let(:story) {double(Story)}
before do
allow(Story).to receive(:all).and_return(story)
allow(story).to
我希望能够简单地测试一个方法是否在另一个方法中被调用,而不需要测试其他任何东西。
让我们假设我有一个进行内部服务调用的方法。
class Foo
def self.perform
result = InternalService.call
...
result.attribute_method # do stuff with result
end
end
InternalService有它自己的所有单元测试,我不想在这里重复这些测试。但是,我仍然需要测试是否正在调用InternalService。
如果我使用Rspec的expect语法,它将模拟出Interna
我正在使用rails,factory_girl_rails和mogoid_rspec宝石.在添加了工厂女孩创业板之后,我一直为我的用户工厂获得错误Factory not registered: user (ArgumentError)。以下是相关的代码片段:
在我的档案里:
group :development, :test do
gem 'byebug'
gem 'rspec-rails'
gem 'mongoid-rspec', '~> 2.1.0'
end
group :test do
gem
我正在遵循Hartl的Ruby on Rails指南,目前我正在尝试设置"guard“和"spork”来自动化"rspec“测试。当我尝试运行bundle exec rspec spec/requests/static_pages_spec.rb时
我得到以下错误:
/home/throne/.rvm/gems/ruby-1.9.3-p392@rails3tutorial2ndEd/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:434:in `require': cannot load such f
请给我一些非常棒的Ruby on Rails 3应用程序,我可以使用它,比如在Rails 3中的最佳实践的例子。我的想法是我想在我的硬盘上有这个应用程序,当我有任何问题如何实现某些功能时,我可以作为参考。它可以是任何博客引擎,或者像Redmine这样的应用程序无关紧要,但它应该具有几乎所有用于任何web应用程序的元素/功能。从Ruby和Rails3的角度来看,应该使用最佳实践来完成。