设计辅助方法,确认!无法在出厂设置中使用/找到

发布于 2024-12-05 20:51:59 字数 2287 浏览 3 评论 0原文

所有,

  • 语言:Ruby on Rails
  • 版本:rails 3
  • 有问题的宝石:devise和factory_girl_rails

我提到了https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-(and-rspec) 并尝试过使用 Factory Girl 来设置管理员和常规设备用户以获得我的控制器规格。我正在设计中使用可确认模块。

在spec/support/controller_macros.rb中,我调用confirm!从工厂获取用户对象后。 代码/注释说,

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

当我在用户工厂、spec/factories/users.rb 中设置“confirmed_at”的值并运行我的控制器规格(rake spec:controllers)时,它可以工作,但是当我不设置“的值时confirmed_at”并依赖confirm!在spec/support/controller_macros.rb中,似乎确认了!没有找到。

我什至尝试将“include Devise::TestHelpers”放入 spec/support/controller_macros.rb 文件但仍然失败。

我得到的错误是

 失败/错误:无法从回溯中找到匹配行
 无方法错误:
   未定义方法“确认!”对于#<用户:0x71d9c7901e48>
 # ./spec/support/controller_macros.rb:8:在`login_user'中

我的spec/spec_helper.rb 内容如下。

-----------spec/spec_helper.rb - 开始 ----------------------------

    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 - 结束 ----------------------------------------

可能是哪里出了问题导致确认!找不到/工作?

谢谢 :)

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_helper.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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的海 2024-12-12 20:51:59

确认!如果您使用 :confirmable 设备模块,那么这是必要的。

在您的 user.rb(设计模型)中,将 :confirmable 放入您的设计选项中,或者如果您不想使用此模块(此模块向用户发送确认电子邮件。)删除确认!来自你的controller_macros.rb。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文