0
votes

I am trying to make a post request to an action in my controller using rspec, which should return a JSON response. This is my current test code which uses the Facebook Graph Gem. I am unsure of my code:

it "creates JSON format metadata with facebook post social message info about social account" do
  stub_request(:post, "https://graph.facebook.com/test_fixed_origin_id_str/likes").with(:body => {"access_token"=>"this_is_a_test_token"}, :headers => {'Content-Type'=>'application/x-www-form-urlencoded'}).to_return(:status => 200, :body => "", :headers => {})
  post :like, id: combined_id, format: :json
end

What would be the best way to make a post request to an API? I apologize if my question is not too clear but I have little experience with making requests in tests.

Thanks

1

1 Answers

0
votes

One thing I can recommend is to just use the VCR gem (https://github.com/vcr/vcr). Essentially, the main idea behind this is that when your tests run the first time, the vcr will "record" the JSON response from the API endpoint.

In next iterations of the test, it will simply "play back" this response instead of making a live call to the server everytime. This has its benefits (speed, simplicity), but also you have to keep in mind that if the API response does change over time (ie. if FB changed their response), you would have to make sure to re-record what is coming back.