无法弄清楚我的测试失败的原因(工厂女孩)

时间:2014-01-21 00:14:32

标签: ruby-on-rails rspec factory-bot

我正在使用rails教程,我正在使用第7章。我正处于需要将factory_girl_rails添加到gemfile中的部分。我安装它但spec文件夹中没有factories.rb。所以我手动创建一个并为其编写代码

require 'factory_girl'

FactoryGirl.define do
  factory :user do
    name     "Michael Hartl"
    email    "michael@example.com"
    password "foobar"
    password_confirmation "foobar"
  end
end

并将其添加到我的测试中。

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) } #this was what I added
    before { visit_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

但是当我运行测试时,我会重复以下错误。

Failures:

  1) User pages profile page 
     Failure/Error: before { visit_path(user) }
     NoMethodError:
       undefined method `visit_path' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_1:0x000001068226f0>
     # ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

  2) User pages profile page 
     Failure/Error: before { visit_path(user) }
     NoMethodError:
       undefined method `visit_path' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_1:0x00000101ff0058>
     # ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

Finished in 0.25449 seconds
33 examples, 2 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:11 # User pages profile page 
rspec ./spec/requests/user_pages_spec.rb:12 # User pages profile page 

从我可以收集到的失败来自

   describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit_path(user) }

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end

我无法弄清楚出了什么问题。如果这有帮助的话,这也是我的gemfile。

source 'https://rubygems.org'
ruby '2.1.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.2'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.1.2'

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'
  gem 'factory_girl_rails', '4.2.1'
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

1 个答案:

答案 0 :(得分:0)

看起来错误就在这里

before { visit_path(user) }

这应该是

before { visit user_path(user) }