使用pickle与cucumber和factory_girl创建关联模型并将参数传递给嵌套模型

发布于 2024-08-20 08:22:09 字数 981 浏览 7 评论 0原文

我有以下模型:

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 技术交流群。

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

发布评论

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

评论(1

终难遇 2024-08-27 08:22:09

我的建议是以更具声明性的风格编写您的步骤,并避免将脆弱的附带细节放入您的场景中。

以下是一些参考资料:

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:

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