FactoryGirl 有很多经历
好的,我已经为用户、项目以及观看这些项目的能力建立了多对多关联。 (这不是我想出的最好的名字“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,您正在使用旧的 FactoryGirl 语法进行工厂定义。
尝试以下操作:
这应该告诉监视工厂
watched_project
实际上是project
类型。使用新语法,您的完整工厂定义将如下所示:
看起来更干净。
Note that you are using the older FactoryGirl Syntax for factory definition.
Try the following:
This should tell the watching factory that
watched_project
is actually of typeproject
.With the new syntax your full factory definition would look like:
Which looks much cleaner.
您使用了混乱的命名方法。另外,这里还有一个应该适用于的 Factory Girl 示例:
You use a messy naming approach. Also, also here is the Factory Girl example that should work in: