2010-08-02 54 views
34

你会如何在rSpec中设计Rails 3中的Devise。我有一个UsersControllerUser模型。这两者都与Devise目前有关,我正在编写控制器规格,我真的很难与我的期望,因为设计sign_in真的干扰了作品。rSpec和Rails3中的stubbing设计

任何事情都会有所帮助。

+2

随着设计,你可以完全剔除用户模型: https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs – Flov 2012-09-02 05:49:13

回答

74

我发现它现在很容易做到这一点。 rspec2和devise有问题,但现在已解决。我想你会需要更新你的宝石。然后,你可以写

require 'spec_helper' 

describe DoStuffController do 
    include Devise::TestHelpers 

    before (:each) do 
    @user = Factory.create(:user) 
    sign_in @user 
    end 

    describe "GET 'index'" do 
    it "should be successful" do 
     get 'index' 
     response.should be_success 
    end 
    end 
end 

[更新]在色器件维基现在有一个详细的(甚至更多先进的日期)description

+7

正如向其他人可能会遇到此问题的说明。如果您使用确认,请确保您的工厂定义:confirmed_at或您将其传入。 – RyanJM 2011-07-22 17:14:19

+2

在设计wiki上有详细说明:https://github.com/plataformatec/devise/wiki/How-To:-Controllers -and-Views-tests-with-Rails-3-(and-rspec) – 2011-11-13 14:55:42

+0

@KevinBedell,URL几乎是正确的 - 它缺少最后一个')'。因此,正确的URL是[this](https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29) – Zabba 2012-05-05 20:02:45

9

您可以尝试嘲弄底层监狱长其设计依赖于,这里是一些细节的链接,您如何使用RSpec做到这一点(http://wiki.github.com/hassox/warden/)对象:http://www.michaelharrison.ws/weblog/?p=349(条目包括一些其他的话题,以及,你想要的解决方案是朝向页面的底部。)

+0

非常感谢!正是我一直在寻找的。 – stuartc 2010-08-05 06:54:09

+0

下面的新答案提供了一个更好的方法http://stackoverflow.com/questions/3387485/stubbing-devise-in-rspec-and-rails3/3512813#3512813 – 2010-10-20 10:25:56