【发布时间】:2023-03-31 02:53:01
【问题描述】:
对于这种情况,我找不到任何东西。我有一个这样定义命名范围的模型:
class Customer < ActiveRecord::Base
# ...
named_scope :active_customers, :conditions => { :active => true }
end
我正试图在我的控制器规范中将它存根:
# spec/customers_controller_spec.rb
describe CustomersController do
before(:each) do
Customer.stub_chain(:active_customers).and_return(@customers = mock([Customer]))
end
it "should retrieve a list of all customers" do
get :index
response.should be_success
Customer.should_receive(:active_customers).and_return(@customers)
end
end
这不起作用并且失败了,说客户期望 active_customers 但收到了 0 次。在我的索引操作的实际控制器中,我有@customers = Customer.active_customers。我缺少什么让这个工作?可悲的是,我发现编写代码比考虑测试/规范并编写代码更容易,因为我知道规范描述的是什么,而不是如何告诉 RSpec 我想做什么。
【问题讨论】:
标签: ruby-on-rails rspec