我有以下规格...
describe "successful POST on /user/create" do
it "should redirect to dashboard" do
post '/user/create', {
:name => "dave",
:email => "[email protected] /cdn-cgi/l/email-protection",
:password => "another_pass"
}
last_response.should be_redirect
follow_redirect!
last_request.url.should == 'http://example.org/dave/dashboard'
end
end
Sinatra 应用程序上的 post 方法使用rest-client 调用外部服务。我需要以某种方式存根其余客户端调用以发回预设响应,这样我就不必调用实际的 HTTP 调用。
我的应用程序代码是...
post '/user/create' do
user_name = params[:name]
response = RestClient.post('http://localhost:1885/api/users/', params.to_json, :content_type => :json, :accept => :json)
if response.code == 200
redirect to "/#{user_name}/dashboard"
else
raise response.to_s
end
end
有人可以告诉我如何使用 RSpec 做到这一点吗?我用谷歌搜索了一下,发现很多博客文章都触及了表面,但我实际上找不到答案。我对 RSpec 时期还很陌生。
Thanks
Using a mock https://relishapp.com/rspec/rspec-mocks/docs对于响应,你可以这样做。一般来说,我对 rspec 和测试还很陌生,但这对我有用。
describe "successful POST on /user/create" do
it "should redirect to dashboard" do
RestClient = double
response = double
response.stub(:code) { 200 }
RestClient.stub(:post) { response }
post '/user/create', {
:name => "dave",
:email => "[email protected] /cdn-cgi/l/email-protection",
:password => "another_pass"
}
last_response.should be_redirect
follow_redirect!
last_request.url.should == 'http://example.org/dave/dashboard'
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)