FactoryGirl 有很多经历

发布于 2024-12-22 20:57:24 字数 1537 浏览 1 评论 0原文

好的,我已经为用户、项目以及观看这些项目的能力建立了多对多关联。 (这不是我想出的最好的名字“Watchings”)

#project.rb
class Project < ActiveRecord::Base
  has_many :watchings, :foreign_key => "watched_project_id"
  has_many :watchers, :through => :watchings, :source => :user 
end

#user.rb
class User < ActiveRecord::Base
  has_many :watchings
  has_many :watched_projects, :through => :watchings
end

#watching.rb
class Watching < ActiveRecord::Base
  belongs_to :user
  belongs_to :watched_project, :class_name => 'Project'
end

到目前为止,功能上一切正常。我可以观看项目并了解项目有多少观看者。但我的黄瓜功能一直出错,因为我不知道如何让工厂女孩配合。

Factory.define :user do |user|
  user.sequence(:email) {|n| "user#{n}@example.com"}
  user.password "secret"
  user.password_confirmation {|u| u.password }
end

Factory.define :project do |project|
  project.name "Project"
  project.description "Hello World"
end

Factory.define :watchings do |watching|
  watching.association(:user)
  watching.association(:watched_project)
end

我的步骤定义:

Given /^a Project exists called "(.+)" with (\d+) watchers$/ do |project, watchers|
  @project = Factory(:project)
  watchers.to_i.times do |watcher|
    watcher = Factory(:user)
    watching = Factory(:watching)
    watching.user = watcher
    watching.watched_project = @project
  end
end

每当步骤定义运行时,它都会惊慌失措,并说

undefined method `watching_user_id=' for #<Watching:0x007ff7800c7978> (NoMethodError)

我已经尝试了一些不同的事情,但我不知道如何使其真正与测试建立关联。

Okay, so i've set up a many-to-many association for users, projects and the ability to watch those projects. (Called, not the best name I've ever come up with "Watchings")

#project.rb
class Project < ActiveRecord::Base
  has_many :watchings, :foreign_key => "watched_project_id"
  has_many :watchers, :through => :watchings, :source => :user 
end

#user.rb
class User < ActiveRecord::Base
  has_many :watchings
  has_many :watched_projects, :through => :watchings
end

#watching.rb
class Watching < ActiveRecord::Base
  belongs_to :user
  belongs_to :watched_project, :class_name => 'Project'
end

Functionally everything is working so far. I can watch projects and find out how many watchers a project has. But my cucumber feature keeps erroring out because I don't know how to make Factory Girl cooperate.

Factory.define :user do |user|
  user.sequence(:email) {|n| "user#{n}@example.com"}
  user.password "secret"
  user.password_confirmation {|u| u.password }
end

Factory.define :project do |project|
  project.name "Project"
  project.description "Hello World"
end

Factory.define :watchings do |watching|
  watching.association(:user)
  watching.association(:watched_project)
end

And my step definition:

Given /^a Project exists called "(.+)" with (\d+) watchers$/ do |project, watchers|
  @project = Factory(:project)
  watchers.to_i.times do |watcher|
    watcher = Factory(:user)
    watching = Factory(:watching)
    watching.user = watcher
    watching.watched_project = @project
  end
end

Whenever the step definition runs, it panics out and says

undefined method `watching_user_id=' for #<Watching:0x007ff7800c7978> (NoMethodError)

I've tried a few different things, but I can't figure out how to make it actually make the association for the test.

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

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

发布评论

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

评论(2

誰認得朕 2024-12-29 20:57:24

请注意,您正在使用旧的 FactoryGirl 语法进行工厂定义。

尝试以下操作:

Factory.define :watchings do |watching|
  watching.association(:user)
  watching.association(:watched_project), :factory => :project
end

这应该告诉监视工厂 watched_project 实际上是 project 类型。

使用新语法,您的完整工厂定义将如下所示:

FactoryGirl.define do
  factory :user do
    sequence(:email) {|n| "user#{n}@example.com"}
    password "secret"
    password_confirmation {|u| u.password }
  end

  factory :project do
    name "Project"
    description "Hello World"
  end

  factory :watchings do
    user
    association :watched_project, :factory => :project
  end
end

看起来更干净。

Note that you are using the older FactoryGirl Syntax for factory definition.

Try the following:

Factory.define :watchings do |watching|
  watching.association(:user)
  watching.association(:watched_project), :factory => :project
end

This should tell the watching factory that watched_project is actually of type project.

With the new syntax your full factory definition would look like:

FactoryGirl.define do
  factory :user do
    sequence(:email) {|n| "user#{n}@example.com"}
    password "secret"
    password_confirmation {|u| u.password }
  end

  factory :project do
    name "Project"
    description "Hello World"
  end

  factory :watchings do
    user
    association :watched_project, :factory => :project
  end
end

Which looks much cleaner.

厌倦 2024-12-29 20:57:24

您使用了混乱的命名方法。另外,这里还有一个应该适用于的 Factory Girl 示例:

FactoryGirl.define do

  factory :project do
    name "Project"
    description "Hello World"
  end

  factory :user do
    sequence(:email) { |n| "user#{n}@example.com" }
    sequence(:password) { |n| "password#{n}" }
  end

  factory :watching do
    user
    project
  end        
end

You use a messy naming approach. Also, also here is the Factory Girl example that should work in:

FactoryGirl.define do

  factory :project do
    name "Project"
    description "Hello World"
  end

  factory :user do
    sequence(:email) { |n| "user#{n}@example.com" }
    sequence(:password) { |n| "password#{n}" }
  end

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