English 中文(简体)
Stubbing Devise in rSpec and Rails3
原标题:

How would you stub Devise in Rails 3 with rSpec. I have a UsersController and a User model. Both of which are associated with Devise at the moment, I m writing controller specs and I really am having a hard time with my expectations as the Devise sign_in is really jamming up the works.

Any thing will help.

最佳回答

I found that it is now pretty easy to do this. There was a problem with rspec2 and devise, but is now solved. I guess you would need to update your gems. Then you can write

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

[UPDATE] On the devise wiki there is now a detailed (and probably more up-to-date) description.

问题回答

You can try mocking the underlying warden (https://github.com/wardencommunity/warden/wiki) object which devise relies upon, here is a link to some details on how you can accomplish this with RSpec: http://www.michaelharrison.ws/weblog/?p=349 (entry covers some other topics as well, the solution you want is towards the bottom of the page.)





相关问题
How to use rspec to test named routes?

Given I have a named route: map.some_route /some_routes/:id , :controller => some , :action => other How do I use the routing spec file spec/routing/some_routing_spec.rb to test for that ...

Silencing Factory Girl logging

Just to clear the air, I am not some cruel factory master trying to silence working ladies. I am having a very annoying problem where when using Thoughtbot s factory girl in my specs, every time ...

Controller specs in isolation mode and render :update

I am using RSpec for writing my controller tests/specs. I faced the problem, that the following code gets rendered: render :update do |page| page[ middle_content ].replace_html :partial => "...

rspec testing views with internationalization?

I want to make sure I have the right meta desc/keyword and title text in my view so I want to create rspec views tests for that. Now the real challange here is how to make it work across multiple ...

热门标签