rspec testing a controller post changing my params from symbols to strings and breaking my tests

后端 未结 2 1584
野的像风
野的像风 2021-02-07 18:54

In my controller spec I am doing this:

it \"should create new message\" do
  Client.should_receive(:create).with({:title => \'Mr\'})
  post \'create\' , :clie         


        
相关标签:
2条回答
  • 2021-02-07 19:36

    @ssmithone you could use ActiveSupport::HashWithIndifferentAccess to pass params as symbols instead of strings. This should work:

    it "should create new message" do
      Client.should_receive(:create).with({:title => 'Mr'}.with_indifferent_access)
      post 'create', :client => {:title => "Mr"}
    end
    
    0 讨论(0)
  • 2021-02-07 20:01

    It's because you are passing a symbol and not a string. This should fix it :

    it "should create new message" do
      Client.should_receive(:create).with({:title => 'Mr'})
      post 'create' , :client => {"title" => "Mr" }
    end
    

    Here's a blogpost about it: "Understanding Ruby Symbols"

    0 讨论(0)
提交回复
热议问题