【问题标题】:how to use devise current_user inside factory_girl factories如何在 factory_girl 工厂中使用设计 current_user
【发布时间】:2011-10-11 20:34:32
【问题描述】:

我想在我使用 factory_girl 创建的工厂中使用 current_user(设计的)。

我的环境包含:Ruby 1.8.7 / Rails 2.3.5 / Devise / Cucumber / Pickle / Factory Girl

我有以下型号

用户:

  has_many :for_me, :class_name => "Task", ....details
  has_many :from_me, :class_name => "Task", ....details

任务:

  belongs_to :assigner, :class_name => "User", ....details
  belongs_to :assignee, :class_name => "User", ....details

我正在测试以下场景:

Given the following users exist
      | email    | password |
      | u1@d.com | 123456   |
      | u1@d.com | 123456   |
And users are confirmed
And I am on the sign in page
And I am logged in as "u1@d.com" with password "123456"
When I fill in "task[description]" with "description 1"
And I press "task_submit"
Then I should see "description 1"

我拥有的工厂是:

Factory.define :user do |f|
  f.first_name          'user_fn'
  f.last_name           'user_ln'
  f.sequence(:username) {|n| "user_#{n}"}
  f.sequence(:email)    {|n| "user_#{n}@domain.com"}
  f.password            '123456'
  f.mobile_number       '0123456798'
end

Factory.define :task do |f|
  f.association :assignee, xxxxxxx
  f.association :assigner, :factory => :user
end

我的问题是我想使用 devise 的 current_user 而不是 xxxxxxx

【问题讨论】:

    标签: ruby-on-rails devise cucumber factory-bot


    【解决方案1】:

    我猜你不能直接在工厂内编码。

    试试这个:

    Factory(:task, :assignee => current_user)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-15
      • 2016-02-01
      • 2013-05-11
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多