0

我是 Rspec 的新手,我读了《rails 3 in action》一书,在那里我进行了 Rspec 测试:

规格/控制器/proect_controller_spec:

require 'spec_helper'

describe ProjectsController do
  let(:user) do
    user = Factory(:user)
    user.confirm!
    user
  end

  let(:project) { Factory(:project) }

  context "standard users" do

    it "cannot access the show action" do
      sign_in(:user, user)
    end

  end
end

规范/工厂/user_factory.rb

Factory.define :user do |user|
  user.sequence(:email) { |n| "user#{n}@ticketee.com" }
  user.password "password"
  user.password_confirmation "password"
end

规范/支持/factories.rb

Dir[Rails.root + "factories/*.rb"].each do |file|
  require file
end

规格/规格助手

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require "email_spec"

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
end

宝石文件

source 'https://rubygems.org'

gem 'rails', '3.2.9'
gem 'pg'
gem 'devise'

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'dynamic_form'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

group :test, :development do
  gem 'rspec-rails', '~> 2.5'
end

group :test do
  gem 'capybara'
  gem 'database_cleaner'
  gem 'factory_girl'
  gem 'email_spec' # 122 page, for email confirmation
end

当我开始时: rspec ./spec/controllers/projects_controller_spec.rb

我有:

 ProjectsController standard users cannot access the show action
     Failure/Error: user = Factory(:user)
     NoMethodError:
       undefined method `Factory' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x8e96710>

我错在哪里了?...

4

2 回答 2

2

我想你想要“factory_girl_rails”,而不是“factory_girl”:https ://github.com/thoughtbot/factory_girl_rails

于 2012-12-16T14:32:35.310 回答
1

FactoryGirl 的语法已经改变,现在你不能再写了

Factory(:user)

但你应该写

FactoryGirl.create(:user)

有关详细信息,请参阅入门文档。有关升级到 3.0 版(包括弃用旧语法)的更多信息,请参阅这篇博文

于 2012-12-16T15:15:33.000 回答