使用pickle与cucumber和factory_girl创建关联模型并将参数传递给嵌套模型
我有以下模型:
class User < ActiveRecord::Base
has_one :profile, :dependent => :destroy
def before_create
self.profile ||= Profile.new
end
end
class Profile < ActiveRecord::Base
belongs_to :user
validates_uniqueness_of :name
end
我有以下工厂:
Factory.define :user do |user|
user.email { Factory.next :email }
user.association :profile
end
Factory.define :profile do |profile|
profile.name 'Name'
end
这就是我的功能:
Given a profile: "John" exists with name: "John"
And a user: "John" exists with profile: profile "John"
有没有办法改进这个?我希望能够写出这样的东西:
Given a user: "John" exists with a profile: profile "John" exists with name: "John"
它创建了一些类似的东西:
Factory(:user, :profile => Factory(:profile, :name) )
它几乎是我需要一个嵌套的匹配器。您能为此建议一个步骤吗?
或者你能建议一种替代方法来实现这一目标吗?
I have the following models:
class User < ActiveRecord::Base
has_one :profile, :dependent => :destroy
def before_create
self.profile ||= Profile.new
end
end
class Profile < ActiveRecord::Base
belongs_to :user
validates_uniqueness_of :name
end
And I have the following factories:
Factory.define :user do |user|
user.email { Factory.next :email }
user.association :profile
end
Factory.define :profile do |profile|
profile.name 'Name'
end
So this is my feature:
Given a profile: "John" exists with name: "John"
And a user: "John" exists with profile: profile "John"
Is there a way I can improve this? I would like to be able to write something like this:
Given a user: "John" exists with a profile: profile "John" exists with name: "John"
And it creates something along the lines of:
Factory(:user, :profile => Factory(:profile, :name) )
Its almost that I need a nested matcher. Can you suggest a step for this?
Or can you suggest an alternative way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议是以更具声明性的风格编写您的步骤,并避免将脆弱的附带细节放入您的场景中。
以下是一些参考资料:
My suggestion is to write your steps in a more declarative style, and avoid putting brittle incidental details into your scenarios.
Here are some references: