我正在尝试测试我的应用程序和遥控器。我使用 Rails 3.2 和最新版本的 vcr 和 fakeweb。实际上我看过RailsCast,现在我想做反测试,但我做不到。
我的请求测试文件;
require "spec_helper"
describe "ZipCodeLookup" do
it "show Beverly Hills given 90210", :vcr do
visit root_path
fill_in "zip_code", with: "90210"
click_on "Lookup"
page.should have_content("Beverly Hills")
end
it "any errors" do
visit root_path
fill_in "zip_code", with: "9"
click_on "Lookup"
page.should have_content("Nomethods")
end
end
不要卡在 page.should 行。我只是尝试了如何进行反测试。
和我的模型;
class ZipCode
attr_reader :state, :city, :area_code, :time_zone
def initialize(zip)
client = Savon::Client.new("http://www.webservicex.net/uszip.asmx?WSDL")
response = client.request :web, :get_info_by_zip, body: { "USZip" => zip }
data = response.to_hash[:get_info_by_zip_response][:get_info_by_zip_result][:new_data_set][:table]
@state = data[:state]
@city = data[:city]
@area_code = data[:area_code]
@time_zone = data[:time_zone]
end
end
我运行时的错误bundle exec rspec spec/requests/zip_code_lookup_spec.rb
Ender-iMac:zip_coder-after ender$ bundle exec rspec spec/requests/zip_code_lookup_spec.rb
HTTPI executes HTTP GET using the net_http adapter
SOAP request: http://www.webservicex.net/uszip.asmx
SOAPAction: "http://www.webserviceX.NET/GetInfoByZIP", Content-Type: text/xml;charset=UTF-8, Content-Length: 389
<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://www.webserviceX.NET" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="http://www.webserviceX.NET"><env:Body><ins0:GetInfoByZIP><ins0:USZip>90210</ins0:USZip></ins0:GetInfoByZIP></env:Body></env:Envelope>
HTTPI executes HTTP POST using the net_http adapter
SOAP response (status 200):
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetInfoByZIPResponse xmlns="http://www.webserviceX.NET"><GetInfoByZIPResult><NewDataSet xmlns=""><Table><CITY>Beverly Hills</CITY><STATE>CA</STATE><ZIP>90210</ZIP><AREA_CODE>310</AREA_CODE><TIME_ZONE>P</TIME_ZONE></Table></NewDataSet></GetInfoByZIPResult></GetInfoByZIPResponse></soap:Body></soap:Envelope>
.HTTPI executes HTTP GET using the net_http adapter
F
Failures:
1) ZipCodeLookup any errors
Failure/Error: click_on "Lookup"
FakeWeb::NetConnectNotAllowedError:
Real HTTP connections are disabled. Unregistered request: GET http://www.webservicex.net/uszip.asmx?WSDL. You can use VCR to automatically record this request and replay it later. For more details, visit the VCR documentation at: http://relishapp.com/myronmarston/vcr/v/1-11-3
# ./app/models/zip_code.rb:6:in `initialize'
# ./app/controllers/zip_code_lookup_controller.rb:3:in `new'
# ./app/controllers/zip_code_lookup_controller.rb:3:in `index'
# (eval):2:in `click_on'
# ./spec/requests/zip_code_lookup_spec.rb:15:in `block (2 levels) in <top (required)>'
Finished in 0.17138 seconds
2 examples, 1 failure
Failed examples:
rspec ./spec/requests/zip_code_lookup_spec.rb:12 # ZipCodeLookup any errors
我不明白为什么 VCR 不能在不同的状态下运行相同的 HTTP 请求。
如何以反测试方式进行测试?
解决方案
抱歉,这是我的错误:(我忘了在“it”描述行中写:vcr。应该是这样的
it "any errors", :vcr do
visit root_path
fill_in "zip_code", with: "9"
click_on "Lookup"
page.should have_content("Nomethods")
end