我的Capapara RSPEC測試有一些困難,我有一個嵌套的資源。這裏的錯誤我收到:水豚+ Rspec嵌套的路線
1) Photo pages visit photo path Photo can be visited after FactoryGirl create records ←[31mFailure/Error:←[0m ←[31mvisit mountain_photo_path(mountain, photo)←[0m
←[31mActiveRecord::RecordNotFound:←[0m ←[31mCouldn't find Photo with id=1 [WHERE "photos"."mountain_id" = 1]←[0m
←[36m # C:in
find'←[0m ←[36m # ./app/controllers/photos_controller.rb:14:in
show'←[0m ←[36m # ./spec/requests/photo_pages_spec.rb:34:in `block (3 levels) in '←[0m
我嵌套的路線如下:
resources :mountains do
resources :photos
end
而且我測試我RSPEC測試中的以下內容:
require 'spec_helper'
describe "Photo pages" do
let(:user) { FactoryGirl.create(:user)}
let(:region) { FactoryGirl.create(:region)}
let(:mountain) {FactoryGirl.create(:mountain)}
let(:photo) {FactoryGirl.create(:photo)}
before { sign_in user }
describe "visit mountain path" do
it "can be visited after FactoryGirl create" do
visit mountain_path(mountain)
page.should have_selector('h1', text: "Breck Test")
end
it "has the correct region associated to it" do
visit mountain_path(mountain)
page.should have_selector('h4', text: "Rockies")
end
end
describe "visit photo path" do
it "Photo can be visited after FactoryGirl create records" do
visit mountain_photo_path(mountain, photo)
page.should have_selector('title', text: photo.name)
end
end
end
我相信FactoryGirl是成功創建所有記錄。 Photo上的附件是通過CarrierWave完成的,並且在調試之後相信這個加載也是正確的。
include ActionDispatch::TestProcess
FactoryGirl.define do
factory :user do
first_name 'Test'
last_name 'User'
email '[email protected]'
password 'password1'
password_confirmation 'password1'
# required if the Devise Confirmable module is used
confirmed_at Time.now
end
factory :region do
name 'Rockies'
end
factory :mountain do
name 'Breck Test'
region {|a| a.association(:region)}
description 'This is my testing mountain only'
end
factory :photo do
name 'My Photo Test'
description 'My lovely description'
mountain {|a| a.association(:mountain)}
image { fixture_file_upload(Rails.root + 'spec/fixtures/files/breck.jpg', "image/jpeg")}
end
end
我非常感謝小組的智慧,在此先感謝您的幫助。今天用這個rspec代碼花了好幾個小時,希望將來它變得更容易。
謝謝您的回答。我嘗試了上面的代碼,但它不起作用,似乎並沒有在FactoryGirl設置中創建照片。我認爲當前的FactoryGirl文件將涉及這兩個記錄,它成功地在Region和Mountain之間進行了測試,併成功通過我添加到上述說明中的代碼進行了測試 – 2013-04-04 19:45:06
似乎我太快地讀了您的工廠,只是編輯了我的答案 – apneadiving 2013-04-04 20:03:37
那樣做了它!非常感謝 :) – 2013-04-04 20:34:36