嘲弄不适用于RSpec和Devise [英] Mocks aren't working with RSpec and Devise
问题描述
目前,我正在使用RSpec 2开发Rails 3网络应用,而我们正在使用Devise进行身份验证。我们的一个(很快就会有很多)控制器要求用户登录。我知道Devise提供了 sign_in
测试助手,但它可以与RSpec或Mocha模拟程序一起使用对象?
I'm working on a Rails 3 web app at the moment with RSpec 2 and we're using Devise for authentication. One (and soon many) of our controllers require the user to be logged in. I know Devise provides the sign_in
test helper, but can it be used with an RSpec or Mocha mock object?
我最初尝试使用 @user = mock_model(User)
,其中user是Devise类。 sign_in:user,@user
不起作用,因为 get'index'
将重定向到登录表单。
I originally tried @user = mock_model(User)
where user is the Devise class. This wouldn't work with sign_in :user, @user
as get 'index'
would redirect to the sign in form.
任何人都具有使用Devise进行测试的经验并可以提供帮助吗?
Does anyone have any experience testing with Devise and can help?
推荐答案
我们遇到了类似的问题,但使用的是Factory Girl。我们这样解决了它:
We had a similar problem, but using Factory Girl. We solved it like so:
在spec_helper.rb中:
In spec_helper.rb:
config.include Devise::TestHelpers, :type => :controller
在控制器规范中(只是包装方法):
In the controller spec (just a wrapper method):
def login_user(user)
sign_in user
end
然后在您需要的每种方法中,都可以执行以下操作:
Then in each method you require, you can do:
login_user(Factory(:user))
...,您在factory.rb中定义了一个用户对象。不知道这是否能与模拟游戏一起使用。
... where you have defined a user object in factories.rb. Not sure if this will work with mocks though.
这篇关于嘲弄不适用于RSpec和Devise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!