Rails JSON API testing POST request with PARAMS in JSON

前端 未结 2 1771
甜味超标
甜味超标 2021-01-04 15:57

This is a problem that has been bothering me for some time. I am building an API function that should receive data in json and response in json. My controller tests run fine

相关标签:
2条回答
  • 2021-01-04 16:46
    describe '#create' do 
      let(:email) {'andre'}
      let(:attrs) {{email: email}}
      let(:params) {{format: :json, subscription: attrs}}
    
      it "should return an error if there is no correct email" do
        post "/magazine_subscriptions", params
      end
    end
    
    0 讨论(0)
  • 2021-01-04 16:49

    I found my answer on a post here(RSpec request test merges hashes in array in POST JSON params), I think what I was doing wrong concerned the arguments to the request.

    so this worked:

    it "should return an error if there is no correct email" do
        params = {:subscription => {:email => "andre"}}
    
        post "/magazine_subscriptions", params.to_json, {'ACCEPT' => "application/json", 'CONTENT_TYPE' => 'application/json'}
    end
    
    0 讨论(0)
提交回复
热议问题