在黄瓜步骤中获取 Factorygirl 创建的对象

发布于 2024-11-30 14:52:41 字数 268 浏览 3 评论 0 原文

我使用黄瓜和工厂女孩,效果非常好,但我发现语法有点强迫。

假设我的模型有有效的工厂,我想要一个通用的黄瓜助手/步骤

Given a model exists
When I visit the model's edit page

,我知道进入页面的步骤部分,但让该步骤知道 #{the model} 引用一个实例,该实例会去在 edit_model_path(@model) 中是我正在寻找的。

一如既往,非常感谢您的帮助。

I'm using cucumber and factory girl with very good results, but I'm finding that the syntax is a little bit forced.

Assuming there are valid factories for my model, I'd like a generic cucumber helper/step

Given a model exists
When I visit the model's edit page

I know the portion of the step going to the page but having the step know that #{the model} is refering to an instance would that would go in edit_model_path(@model) is what I'm looking for.

As always, thanks very much for the help.

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

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

发布评论

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

评论(2

帅的被狗咬 2024-12-07 14:52:41

传递代表类的字符串...因此对于 Thing 类,使用字符串“thing”。一旦你完成了这个步骤,你就可以开始了,我想......

你可以使用类名来构建编辑页面的 URL。您实际上可以使用以下代码从其名称中获取类常量:

my_model = "thing".classify.constantize
my_instance = my_model.find(...)

我认为这可以让您足够概括该步骤,不是吗?

Pass the a string in representing the class...so for the class Thing, use the string 'thing'. Once you have this in a step you should be good to go, I'd think...

You could probably use the class name to build up the URL to your edit page. You can actually get the class constant from it's name using this code :

my_model = "thing".classify.constantize
my_instance = my_model.find(...)

I think this would let you generalize the step enough, no?

二手情话 2024-12-07 14:52:41

您正在寻找的是 pickle,它为您提供了确切的功能。将 gem 添加到 Gemfile 后,运行 rails g pickle --paths --email 命令,您可以编写类似的步骤

# assuming you have an User class and a Factory for that model
Given a user exists
And another user exists with role: "admin"

# later
Then a user should exist with name: "Fred"
And that user should be activated # this uses rspec predicate matchers

对于有关路径的问题,您可以执行此操作(请记住 --paths 生成器中的选项)

Scenario: Show product
    Given a product exists with name: "Milk", price: "2.99"
    When I go to the show page for that product
    Then I should see "Milk" within "h1"
    And I should see "$2.99"

自述文件和此railscast

What you're looking for is pickle, it gives you that exact functionality. After you've added the gem to your Gemfile you run the rails g pickle --paths --email command you can write steps like

# assuming you have an User class and a Factory for that model
Given a user exists
And another user exists with role: "admin"

# later
Then a user should exist with name: "Fred"
And that user should be activated # this uses rspec predicate matchers

For your question about paths you can do this (remember the --paths option in the generator)

Scenario: Show product
    Given a product exists with name: "Milk", price: "2.99"
    When I go to the show page for that product
    Then I should see "Milk" within "h1"
    And I should see "$2.99"

More examples in the readme file and in this railscast

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