我是Rspec的新手,我读过“rails 3 in action”这本书,我有Rspec测试:
规格/ controlers / 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
规格/ spec_helper
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
的Gemfile
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>
我错了?...
答案 0 :(得分:2)
我想你想要“factory_girl_rails”,而不是“factory_girl”:https://github.com/thoughtbot/factory_girl_rails
答案 1 :(得分:1)
FactoryGirl的语法已经改变,现在你再也无法写了
Factory(:user)
但你应该写
FactoryGirl.create(:user)
有关详细信息,请参阅getting started document。 有关升级到3.0版的更多信息(包括旧语法的弃用),请参阅this blogpost。