Having some difficulties with my Capybara RSPEC testing where I have a nested resource. Here's the error I receive:
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
My nested route is as follows:
resources :mountains do
resources :photos
end
And I'm testing the following within my RSPEC test:
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
I believe FactoryGirl is creating all of the records successfully. The attachment on Photo is done through CarrierWave and after debugging believe this is loading correctly as well.
include ActionDispatch::TestProcess
FactoryGirl.define do
factory :user do
first_name 'Test'
last_name 'User'
email 'example@example.com'
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
I greatly appreciate the wisdom of the group here, thanks in advance for your help. Spent several frustrating hours with this rspec code today, hoping it gets easier in the future.
Your issue is that you create a mountain
, then a photo
but they are not related!
Do this instead:
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, mountain: mountain) }
before { sign_in user }
describe "visit photo path" do
it "Photo can be visited after FactoryGirl create records" do
visit mountain_photo_path(photo.mountain, photo)
page.should have_selector('title', text: photo.name)
end
end
end