开发者

devise helper method, confirm! cannot be used/found within factory setup

开发者 https://www.devze.com 2023-04-05 21:31 出处:网络
all, language: Ruby on rails version: rails 3 gems in question: devise and factory_girl_rails I refered to https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rai

all,

  • language: Ruby on rails
  • version: rails 3
  • gems in question: devise and factory_girl_rails

I refered to https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-(and-rspec) and tried to set up admin and regular devise users using factory girl for my controller specs. I am using the confirmable module in devise.

In spec/support/controller_macros.rb, I call confirm! after getting a user object from the factory. The code/comment says

> "user.confirm! # or set a confirmed_at inside the factory. Only
> necessary if you are using the confirmable module"

When I set the value of "confirmed_at" in the users factory, spec/factories/users.rb and run my controller specs (rake spec:controllers), it works BUT when I do not set the value of "confirmed_at" and rely on confirm! in spec/support/controller_macros.rb, it seems that confirm! is not found.

I have even tried putting in " include Devise::TestHelpers" in the spec/support/controller_macros.rb file but still fails.

The error I got was

 Failure/Error: Unable to find matching line from backtrace
 NoMethodError:
   undefined method `confirm!' for #<User:0x71d9c7901e48>
 # ./spec/support/controller_macros.rb:8:in `login_user'

My spec/spec_helpe开发者_如何学Cr.rb reads as follows.

----------- spec/spec_helper.rb - start ----------------------------


    ENV["RAILS_ENV"] ||= 'test'
    require File.expand_path("../../config/environment", __FILE__)
    require 'rspec/rails'
    require 'webrat'

    # Requires supporting ruby files with custom matchers and macros, etc,
    # in spec/support/ and its subdirectories.
    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

    RSpec.configure do |config|
      config.mock_with :rspec
      config.include Devise::TestHelpers, :type => :controller
      config.extend ControllerMacros, :type => :controller

      # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
      config.fixture_path = "#{::Rails.root}/spec/fixtures"

      # If you're not using ActiveRecord, or you'd prefer not to run each of your
      # examples within a transaction, remove the following line or assign false
      # instead of true.
      config.use_transactional_fixtures = true

    end

----------- spec/spec_helper.rb - end ----------------------------

What could be wrong that is causing confirm! not to be found/working?

Thank you :)


The confirm! it's just necessary if you are using the :confirmable devise module.

In your user.rb(devise model) put :confirmable in your devise options or if you don't want use this module(this module send a confirmation email to the user.) remove the confirm! from your controller_macros.rb.

0

精彩评论

暂无评论...
验证码 换一张
取 消