使用ActionController :: Parameters

时间:2017-04-03 10:08:29

标签: ruby-on-rails-5 rspec-rails rspec3

我正在将rails更新为5.0.1并且获得了一个失败的规范

此规范是

describe "POST create", :vcr do
    let(:subscription) { mock_model(Subscription).as_null_object }
    before do
      allow(Subscription).to receive(:new).and_return(subscription)
    end

    it "creates a new Subscription" do
      expect(Subscription).to receive(:new).
        with("card_expiration" => "a quick brown fox").
        and_return(subscription)
      post :create, params: { :subscription => { "card_expiration" => "a quick brown fox" }}
    end
end

失败了

Diff:
       @@ -1,2 +1,2 @@
       -[{"card_expiration"=>"a quick brown fox"}]
       +[<ActionController::Parameters {"card_expiration"=>"a quick brown fox"} permitted: true>]

如何将ActionController :: Parameters纳入期望值?

1 个答案:

答案 0 :(得分:0)

您必须更改期望实际的ActionController :: Parameters对象,如下所示:

 parameters = ActionController::Parameters.new(
        "card_expiration" => "a quick brown fox").permit!

 expect(Subscription).to receive(:new).
        with(parameters).
        and_return(subscription)