我们在controller.should_receive(:before_filter_name)
中使用rspec
通过我们的rails 3.2 rspec中的before过滤器。但对于某些before filter
,rspec会返回错误。
以下是过滤器check_availability
之前的def:
def check_availability
if find_config_const('sales_lead', 'customerx').nil? or find_config_const('sales_lead', 'customerx') == 'false'
redirect_to authentify.signin_path, :notice => "Quit!"
end
end
在控制器中,前过滤器:
before_filter :require_employee
before_filter :load_customer
before_filter :check_availability
要绕过check_availability
,我们将以下代码放在rspec中作为控制器:
before(:each) do
controller.should_receive(:require_signin)
controller.should_receive(:check_availability)
end
过滤器require_signin
没问题。但是对于第二个过滤器,有一个错误:
2) Customerx::SalesLeadsController GET 'new' should redirect user without proper right
←[31mFailure/Error:←[0m ←[31mcontroller.should_receive(:check_availability)←[0m
←[31m(#<Customerx::SalesLeadsController:0x6fb3478>).check_availability(any args)←[0m
←[31m expected: 1 time←[0m
←[31m received: 0 times←[0m
←[36m # ./spec/controllers/customerx/sales_leads_controller_spec.rb:7:in `block (2 levels) in <module:Customerx>'←[0m
我们不太明白should_receive
在这里是如何运作的。我们已经看到了一些像这样的情况,并希望找出导致rspec失败的原因。谢谢你的帮助。