Rspec,Devise,Factory女孩-如何通过Rspec控制器测试登录Factory用户? [英] Rspec, Devise, Factory girl - how to sign in Factory user through Rspec controller test?

查看:71
本文介绍了Rspec,Devise,Factory女孩-如何通过Rspec控制器测试登录Factory用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Posts控制器,我刚刚安装了Devise,可以快速,轻松地添加一些验证。在安装Devise之前,我已经为Posts控制器的 new操作创建了一些测试。安装Devise之后,直到添加

I have a Posts controller and I have just installed Devise to add some authentication quickly and easily. Before I installed Devise, I had created some tests for the 'new' action of my Posts controller. After installing Devise, my tests were failing until i added the

config.include Devise::TestHelpers, :type => :controller

行到我的spec_helper.rb文件。

line to my spec_helper.rb file.

我也有一行

before_filter :authenticate_user!, :except => [:show, :index]

在我的PostsController中。

in my PostsController.

现在,当我运行测试时,它们全部通过,除了以下2个:

Now when I run my tests, they all pass except these 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

我也在使用Factory girl。该错误是由before_filter除了参数引起的。我需要能够使用工厂用户在rspec中登录该用户。如何在Rspec控制器测试中登录Factory用户?如何定义Factory用户?

I'm using Factory girl as well. The error is cause by the before_filter except parameter. I need to be able to sign in the user in rspec with a factory user. How do I sign the Factory user in from within the Rspec controller test? How do I define the Factory user?

我已经在自己的factory.rb中尝试过此操作:

I have tried this in my factories.rb:

Factory.define :user do |user|  
  user.name                  "Test User"
  user.email                 "user@example.com"
  user.password              "password"
  user.password_confirmation "password"
end

但随后出现错误:

NoMethodError:
   undefined method `password_salt=' for #<User:0x00000103cac958>

任何帮助将不胜感激。谢谢。

Any help would be much appreciated. Thanks.

推荐答案

我强烈建议删除您的控制器测试并改用Cucumber故事-RSpec不适用于测试用户-

I highly recommend removing your controller tests and using Cucumber stories instead -- RSpec is not as convenient for testing user-facing stuff as Cucumber is.

在Cucumber中,您希望通过UI登录以模仿用户的操作。我倾向于在控制器规范中做同样的事情,尽管有些人可能更喜欢嘲笑。

In Cucumber, you'd want to log in through the UI to mimic exactly what the user will do. I would tend to do the same thing in a controller spec, though some people might prefer mocking things out instead.

对于未定义的方法password_salt = ,您是否迁移了测试数据库?

As for undefined method password_salt=, did you migrate your test DB?

这篇关于Rspec,Devise,Factory女孩-如何通过Rspec控制器测试登录Factory用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆