Rspec使用post请求获得响应

时间:2013-11-27 21:25:11

标签: ruby-on-rails facebook-graph-api rspec

我正在尝试使用rspec对我的控制器中的操作发出post请求,这应该返回JSON响应。这是我目前使用Facebook Graph Gem的测试代码。我不确定我的代码:

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

向API发布请求的最佳方式是什么?如果我的问题不太明确但我在测试中提出请求的经验不多,我很抱歉。

由于

1 个答案:

答案 0 :(得分:0)

我可以推荐的一件事就是使用VCR gem(https://github.com/vcr/vcr)。从本质上讲,这背后的主要思想是,当您的测试第一次运行时,vcr将“记录”来自API端点的JSON响应。

在测试的下一次迭代中,它将简单地“回放”此响应,而不是每次都对服务器进行实时调用。这有其好处(速度,简单),但你必须记住,如果API响应确实随着时间的推移而改变(即,如果FB改变了他们的响应),你必须确保重新记录即将发生的事情回来。

相关问题