Ruby on rails 带规格和工厂女孩的测试控制器,can';不要登录
我是rails和测试rails应用程序的新手。我尝试用rspec测试一个现有的rails应用程序。Ruby on rails 带规格和工厂女孩的测试控制器,can';不要登录,ruby-on-rails,rspec,devise,factory-bot,functional-testing,Ruby On Rails,Rspec,Devise,Factory Bot,Functional Testing,我是rails和测试rails应用程序的新手。我尝试用rspec测试一个现有的rails应用程序。我刚刚完成了模型测试,还必须完成控制器测试。但是我在rspec上的sign_-in方法有问题。我在internet上尝试了所有解决方案,但仍然无法像使用rspec的用户那样登录。 这是我的控制器代码,太简单了 class AboutController < ApplicationController def index end def how_it_works end
我刚刚完成了模型测试,还必须完成控制器测试。
但是我在rspec上的sign_-in方法有问题。我在internet上尝试了所有解决方案,但仍然无法像使用rspec的用户那样登录。
这是我的控制器代码,太简单了
class AboutController < ApplicationController
def index
end
def how_it_works
end
def what_is
end
def creative_tips
end
def brand_tips
end
def we_are
end
def faq
end
end
这是我的工厂代码
Factory.define :unconfirmed_user, :class => User do |u|
u.sequence(:user_name) {|n| "user_#{n}"}
u.sequence(:email){|n| "user__#{n}@example.com"}
u.sequence(:confirmation_token){|n| "confirm_#{n}"}
u.sequence(:reset_password_token){|n| "password_#{n}"}
u.password '123456'
u.password_confirmation '123456'
u.user_type :nil
end
Factory.define :user, :parent => :unconfirmed_user do |u|
u.confirmed_at '1-1-2010'
u.confirmation_sent_at '1-1-2010'
end
Factory.define :customer, :parent => :user do |u|
u.user_type :customer
end
最后,这里是my spec_帮助程序代码
ENV["RAILS_ENV"] ||= 'test'
require File.dirname(__FILE__) + "/../config/environment" unless defined?(Rails)
require 'rspec/rails'
require "paperclip/matchers"
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
RSpec.configure do |config|
config.include Paperclip::Shoulda::Matchers
end
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end
gem文件
gem 'rails', '3.0.3'
gem 'mysql2', '< 0.3'
.
.
.
gem "devise" , "1.1.5"
.
.
.
group :test do
gem 'shoulda'
gem "rspec-rails", "~> 2.11.0"
gem "factory_girl_rails"
end
gem'rails','3.0.3'
gem“mysql2”,“小于0.3”
.
.
.
创业板“设计”、“1.1.5”
.
.
.
组:测试do
宝石“应该”
gem“rspec rails”,“~>2.11.0”
宝石“工厂女孩轨道”
结束
这里是错误
Failures:
1) AboutController index page : should be loaded successfully
Failure/Error: sign_in @customer
NoMethodError:
undefined method `env' for nil:NilClass
# ./spec/controllers/about_controller_spec.rb:7:in `block (2 levels) in <top (required)>'
故障:
1) AboutController索引页:应成功加载
失败/错误:登录@customer
命名错误:
nil:NilClass的未定义方法“env”
#./spec/controllers/about_controller_spec.rb:7:in'block(2层)in'
解决方案一定太简单了,但我是rails新手,我找不到:(您必须在
it
块中进行登录步骤,以便rspec能够访问响应,例如:
it 'should be loaded successfully' do
sign_in @customer
response.should be_success
end
你没有主题
require 'spec_helper'
describe AboutController do
before(:all) do
@customer=Factory.create(:customer)
sign_in @customer
end
context 'index page :' do
subject { Page }
it 'should be loaded successfully' do
response.should be_success
end
end
end
你可能需要
拜访客户\u路径
你不需要定义一个主题来检查响应是否成功。现在它工作了:D谢谢你。但是为什么登录方法在before块上不起作用呢?事实上,我相信如果你把在@customer
中登录到正常的before
块中(而不是before(:all)
块)它也应该工作。before(:all)
在所有测试之前运行,因此我认为不会在第一个测试(或任何其他测试)中进行响应。另外,要明确的是,如果您想检查索引页是否正在加载,您可能需要在检查响应之前添加一个get:index
。它检查的是您的登录@customer
步骤的响应是否成功。我还有一个问题:设置块在我的规范的上下文块中不起作用。如何我能解决这个问题吗?我想你应该发布另一个问题;)
require 'spec_helper'
describe AboutController do
before(:all) do
@customer=Factory.create(:customer)
sign_in @customer
end
context 'index page :' do
subject { Page }
it 'should be loaded successfully' do
response.should be_success
end
end
end