34

你会如何在 Rails 3 中使用 rSpec 存根 Devise。我有一个UsersController和一个User模型。目前这两者都与设计有关,我正在编写控制器规格,我真的很难达到我的期望,因为设计sign_in真的阻碍了工作。

任何事情都会有所帮助。

4

2 回答 2

74

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

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

[更新] 在设计 wiki 上现在有一个详细的(可能是更新的)描述

于 2010-08-18T13:53:44.490 回答
9

您可以尝试模拟设计所依赖的底层守望者 ( https://github.com/wardencommunity/warden/wiki ) 对象,这里是有关如何使用 RSpec 完成此操作的一些详细信息的链接:http://www。 michaelharrison.ws/weblog/?p=349(条目还涵盖了一些其他主题,您想要的解决方案位于页面底部。)

于 2010-08-04T20:13:30.093 回答