我在Michael Hartl的Rails教程的第7.1.3节中,我无法让FactoryGirl在测试中正常工作。它似乎不是FactoryGirl的问题,因为我也尝试在before hook中创建User with User.create并遇到同样的问题。以下是我得到的结果:
我在测试运行时在浏览器中截取了错误的屏幕截图:(无法找到在控制台中查看结果的方法。当我运行测试控制台时,它们没有显示。)
我已重新启动服务器和控制台。并且已经在测试环境中进行了迁移以确保其是最新的。
以下是我的文件。您也可以在https://github.com/cjaypierson/sample_app/tree/sign-up
的github上找到这个规格/请求/ 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
规格/ factories.rb
FactoryGirl.define do
factory :user do
name "Ed Sullivan"
email "ed@example.com"
password "foobar"
password_confirmation "foobar"
end
end
规格/ 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
视图/用户/ show.html.erb
<% provide(:title, @user.name) %>
<h1><%= @user.name %></h1>
模型/ user.rb
class UsersController < ApplicationController
def new
end
def show
@user = User.find(params[:id])
end
end
控制器/ 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
感谢您的帮助。
答案 0 :(得分:0)
我刚刚过了7.1.3章,所以我为你查了一下。
在书中:
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 "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
尝试删除&#39; @&#39;在@user
变量中,如果可行,请告诉我。
答案 1 :(得分:0)
Michael Hartl的教程目前正在使用RSpec 2.13.1。 RSpec 3有许多重大变化,如下所述:https://relishapp.com/rspec/docs/upgrade
如果您正在学习,在完成他的教程时,我强烈建议您使用与他相同的宝石版本:
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