我正在嘗試使用RSpec的請求規範將主機更改爲指向遠程URL而不是localhost:3000。請讓我知道這是否可能。使用RSpec的請求規範測試外部API
注意:只想提及遠程URL只是一個JSON API。
我正在嘗試使用RSpec的請求規範將主機更改爲指向遠程URL而不是localhost:3000。請讓我知道這是否可能。使用RSpec的請求規範測試外部API
注意:只想提及遠程URL只是一個JSON API。
是的,這是可能的
基本上
require 'net/http'
Net::HTTP.get(URI.parse('http://www.google.com'))
# => Google homepage html
但是你可能需要模擬響應,測試最好不要依賴於外部資源。
然後你可以使用模擬的寶石一樣Fakeweb或類似:https://github.com/chrisk/fakeweb
require 'net/http'
require 'fakeweb'
FakeWeb.register_uri(:get, "http://www.google.com", :body => "Hello World!")
describe "external site" do
it "returns 'World' by visiting Google" do
result = Net::HTTP.get(URI.parse('http://www.google.com'))
result.should match("World")
#=> true
end
end
不要緊,你會得到一個正常的HTML響應或JSONP響應。所有相似。
以上是低級別的方法。更好的方法是在應用程序中使用您的代碼來檢查它。但你總是需要模擬。
也許你想要這個:http://stackoverflow.com/questions/12219085/can-i-use-rspec-to-test-deployed-site –