Factory_girl_rails &黄瓜::未定义

发布于 2024-12-19 15:19:26 字数 1544 浏览 1 评论 0原文

进行这项工作时遇到问题。似乎这对其他人来说是一个问题,我想我已经遵循了所有建议。

我已经设置了一个精简的 Rails 3 .0.14 应用程序,仅包含 Cucumber-rails 和 Cucumber-rails 。 Factory_girl_rails 但仍然没有进展。我希望我在做一些愚蠢的事情!

运行下面的 cuc 测试会产生以下结果:

Scenario: test factory-girl        # features/users.feature:3
  Given the following user exists: # features/users.feature:4
    | name    | email               |
    | Brandon | [email protected] |
    Undefined step: "the following user exists:" (Cucumber::Undefined)

用户工厂已创建,我确信它有一些“pp”输出。 非常感谢任何帮助解决这个问题。

Ross

设置

env.rb:代码段

require 'pp'
require 'cucumber/rails'

require 'factory_girl_rails'
require 'factory_girl/step_definitions'

features/support/factories.rb:

FactoryGirl.define do
  factory :user do
    name 'Adam Advertiser'
    email '[email protected]'still
  end   
end
pp FactoryGirl.create(:user)

Cucumber features/user.feature: >

Feature: a
  Scenario: test factory-girl
    Given the following user exists:
    | name    | email               |
    | Brandon | [email protected] |

Having a problem with making this work. Seems it has been a problem for others and I think I have followed all of the advice.

I've set up a stripped down rails 3 .0.14 app to just include cucumber-rails & factory_girl_rails but still no go. I expect I am doing something silly!

Running the cuc test below produces the following:

Scenario: test factory-girl        # features/users.feature:3
  Given the following user exists: # features/users.feature:4
    | name    | email               |
    | Brandon | [email protected] |
    Undefined step: "the following user exists:" (Cucumber::Undefined)

The user factory has been created, of which I am sure, with a bit of 'pp' output.
Would really appreciate any help to get this sorted.

Ross

Set up

env.rb: snippet

require 'pp'
require 'cucumber/rails'

require 'factory_girl_rails'
require 'factory_girl/step_definitions'

features/support/factories.rb:

FactoryGirl.define do
  factory :user do
    name 'Adam Advertiser'
    email '[email protected]'still
  end   
end
pp FactoryGirl.create(:user)

Cucumber features/user.feature:

Feature: a
  Scenario: test factory-girl
    Given the following user exists:
    | name    | email               |
    | Brandon | [email protected] |

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

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

发布评论

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

评论(1

挽手叙旧 2024-12-26 15:19:26

这里的问题是,您必须在要求“factory_girl/step_definitions”之前先获得所需的工厂,因为step_definitions中有元编程需要了解您的工厂。您可以在 env.rb 中显式要求factories.rb,但这最终会产生重复定义错误,因为 cucumber 将重新要求factories.rb。

您需要从 env.rb 中删除对 step_definitions 的要求 - 这会使它发生得太早 - 并将其放在factories.rb的底部,或者创建一个首先需要工厂的包装器(需要驻留在某个地方)黄瓜不会自动需要),然后是step_definitions。

The problem here is that you have to get your factory required before requiring 'factory_girl/step_definitions', because there is meta-programming in step_definitions which needs to know about your factory. You could explicitly require the factories.rb in the env.rb, but that will end up producing a duplicate definition error, as cucumber will re-require factories.rb.

You need to remove the requiring of step_definitions from the env.rb - that will make it happen too early - and put it at the bottom of factories.rb, or else create a wrapper which requires first the factories (which will need to reside somewhere that cucumber doesn't automatically require) and then the step_definitions.

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