我是 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>
我错在哪里了?...