0

我们在 rails 3.2 rspec 中使用controller.should_receive(:before_filter_name)inrspec来绕过 before 过滤器。但是对于某些before filter, rspec 返回错误。

这是过滤器之前的定义check_availability

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 失败的原因。谢谢您的帮助。

4

0 回答 0