我有一个Posts控制器,我刚刚安装了Devise,以便快速轻松地添加一些身份验证。在我安装Devise之前,我为Posts控制器的'新'动作创建了一些测试。安装设计之后,我的测试失败,直到我说的Rspec,设计,工厂女孩 - 如何通过Rspec控制器测试登录工厂用户?
config.include Devise::TestHelpers, :type => :controller
行我spec_helper.rb文件。
我也有我的PostsController行
before_filter :authenticate_user!, :except => [:show, :index]
。
现在,当我跑我的测试中,他们都通过这些除外2:
it "should be successful" do
get :new
response.should be_success
end
it "should have the right title" do
get :new
response.should have_selector('title', :content => @base_title + " | New post")
end
我用厂的姑娘为好。该错误是由before_filter except参数引起的。我需要能够通过工厂用户在rspec中登录用户。如何在Rspec控制器测试中签署Factory用户?我如何定义工厂用户?
我在factories.rb试过这样:
Factory.define :user do |user|
user.name "Test User"
user.email "[email protected]"
user.password "password"
user.password_confirmation "password"
end
但后来我得到的错误:
NoMethodError:
undefined method `password_salt=' for #<User:0x00000103cac958>
任何帮助将非常感激。谢谢。
ahhhh完全忘记迁移测试数据库...现在我得到:'失败/错误:@user = create!(工厂(:用户)) ActiveRecord :: RecordInvalid: 验证失败: t为空,密码不能为空' – Jack
什么代码给出了这个错误? (错误信息应该有一个文件名和行号。) –
这是在我的帖子控制器测试之前(:每个)做 @user = create!(工厂(:用户)) test_sign_in(@user) end' – Jack