[英]How do I test w/rspec Request Spec
我正在嘗試編寫rspec功能/請求以測試以下事實:填寫表單以在“ VIN”模型中創建記錄后,將用戶重定向到剛創建的記錄的edit_path。 這是我目前的測試:
describe "Add VIN" do
it "can be added on the VIN listing page" do
sign_in_as_a_valid_user
vin = FactoryGirl.create(:vin)
current_path.should == vins_path
page.should have_field("vin_text_box")
fill_in "vin_text_box", :with => "hello"
click_button "generate_vin"
current_path.should == edit_vin_path(v.id)) <---- #problem
end
end
明顯的問題是,我的工廠創建的vin與重定向到的vin沒有關系。
所以問題是,如何在請求規范中測試此功能?
您可以從數據庫中獲取最近創建的Vin
。
# ...
click_button "generate_vin"
v = Vin.order("id desc").first
current_path.should eq(edit_vin_path(v.id))
這是RelishApp的示例: https ://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec
require "spec_helper"
describe "Widget management" do
it "creates a Widget and redirects to the Widget's page" do
get "/widgets/new"
expect(response).to render_template(:new)
post "/widgets", :widget => {:name => "My Widget"}
expect(response).to redirect_to(assigns(:widget))
follow_redirect!
expect(response).to render_template(:show)
expect(response.body).to include("Widget was successfully created.")
end
end
看看他們怎么辦? 將:show更改為:edit並測試頁面的完整性,而不是控制器是否選擇了正確的@vin。 您可以通過為FactoryGirl為其屬性之一傳遞特定值來獲得該值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.