Rspec响应在呼叫之间发生变化

时间:2018-01-29 02:19:38

标签: ruby-on-rails ruby rspec

我正在使用以下代码行来检查控制器是否使用参数

呈现响应
expect(controller).to receive(:render).with('orders/order_pickup/layout', {:locals=>{:state=>'pickup'}})

但是当我运行rspec测试时,我得到......

 Diff:
   @@ -1,2 +1,2 @@
   -["orders/order_pickup/layout", {:locals=>{:state=>"pickup"}}]
   +[]

但是,如果我只从rspec代码中删除一个字符:

expect(controller).to receive(:render).with('orders/order_pickup/layout', {:locals=>{:state=>'picku'}})

(注意我刚刚将'locals' - >'state'参数更改为'picku'而没有'p')。这会产生新的错误

Diff:
       @@ -1,2 +1,2 @@
       -["orders/order_pickup/layout", {:locals=>{:state=>"picku"}}]
       +["orders/order_pickup/layout", {:locals=>{:state=>"pickup"}}]

这正是我第一次想到的!当我修复拼写错误时(只需将p添加回来),我再次收到第一个错误...

我很困惑为什么来自rpsec的'实际'响应第一次没有返回args,但第二次返回预测响应?

Rspec代码

 RSpec.describe OrdersController, type: :controller do

    before do
        Order.any_instance.stub(:valid?).and_return(true)
    end

    it "should get order pickup layout if order is present" do
        order = Order.create()
        expect(controller).to receive(:render).with('orders/order_pickup/layout', {:locals=>{:state=>'pickup'}})
        get :order_pickup, :disguised_id => order.disguised_id
    end
end

控制器代码

def order_return
    @order = Order.where(disguised_id:params["disguised_id"]).first
    if @order.present?
        render "orders/order_pickup/layout", :locals => {:state => 'pickup'} 
    else 
        redirect_to "/"
    end
end

0 个答案:

没有答案