I'm in section 7.1.3 of Michael Hartl's Rails Tutorial and I can't get FactoryGirl to work correctly in testing. It doesn't appear to be a problem with FactoryGirl because I also tried creating a User with User.create in a before hook and had the same problem. Here are the results I'm getting:
I took a screenshot of the error in the browser when the test was running: (Couldn't find a way to look at results in console. They did not show when I ran the test console.)
I have restarted the server and console. And have done a migrate on the Test Environment to make sure its up to date.
Below are my files. You can also find this on github at https://github.com/cjaypierson/sample_app/tree/sign-up
spec/requests/user_pages_spec.rb
require 'rails_helper'
describe "User Pages" do
subject { page }
describe "Profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
end
spec/factories.rb
FactoryGirl.define do
factory :user do
name "Ed Sullivan"
email "ed@example.com"
password "foobar"
password_confirmation "foobar"
end
end
spec/spec_helper.rb
require File.expand_path('config/environment.rb')
require 'capybara'
require 'capybara/rails'
require 'capybara/rspec'
require 'factory_girl_rails'
require 'support/utilities.rb'
Capybara.default_driver = :selenium
RSpec.configure do |config|
config.include Capybara::DSL
end
views/users/show.html.erb
<% provide(:title, @user.name) %>
<h1><%= @user.name %></h1>
models/user.rb
class UsersController < ApplicationController
def new
end
def show
@user = User.find(params[:id])
end
end
controllers/users_controller.rb
class UsersController < ApplicationController
def new
end
def show
@user = User.find(params[:id])
end
end
Gemfile
source 'https://rubygems.org'
ruby '2.1.1'
#ruby-gemset=railstutorial_rails_4_0_5
gem 'rails', '4.1.1'
gem 'bootstrap-sass', '2.3.2.0'
gem 'sprockets', '2.11.0'
gem 'bcrypt-ruby', '3.1.2'
gem 'pg'
gem 'heroku_secrets', github: 'alexpeattie/heroku_secrets'
group :development, :test do
gem 'rspec-rails'
gem 'capybara'
end
group :test do
gem 'selenium-webdriver'
gem 'factory_girl_rails'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'rails_12factor', '0.0.2'
end
schema.rb
ActiveRecord::Schema.define(version: 20140708142214) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
end
database.yml
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
encoding: unicode
database: sadevelopment
test:
<<: *default
encoding: unicode
database: satest
Thanks for the help.
I went past chapter 7.1.3 a while ago, so I looked it up for you.
In the book:
describe "Profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
Your code:
describe "Profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(@user) }
it { should have_content(@user.name) }
it { should have_title(@user.name) }
end
Try removing the '@' in the @user
variable, let me know if that works.
Michael Hartl's tutorial is currently using RSpec 2.13.1. RSpec 3 has many breaking changes, as mentioned here: https://relishapp.com/rspec/docs/upgrade
If you're learning, when going through his tutorial I highly recommend that you use the same gem versions that he does:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.8'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.