私は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
スペック/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>
どこを間違えたのでしょうか...