黄瓜:rails 动态查找在 paths.rb 中不起作用

发布于 2024-08-02 12:15:40 字数 1227 浏览 5 评论 0原文

我正在使用 rspec、rspec-rails、cucumber 和 webrat 开发 Rails。 我正在尝试学习 BDD 和一般测试。 我有一个像这样的黄瓜场景:

  Scenario: Questions List
    Given quiz titled "Pearl Jam" has questions named "Corduroy, Dissident"
    When I go to the experiment page for quiz titled "Pearl Jam"
    Then I should see "Corduroy"
    And I should see "Dissident" 

我已经添加了步骤 1,我在其中正确创建并保存了测验(我通过 put 进行了测试)。 现在我正在执行第 2 步。我正在 paths.rb 中添加新路径,

when /^the experiment page for quiz titled "(.*)"$/i 
  new_quiz_experiment_path(Quiz.find_by_title($1))  

由于某种原因 find_by_title 不起作用。我收到此错误消息:

new_quiz_experiment_url failed to generate from {:quiz_id=>nil, :action=>"new", :controller=>"experiments"}, expected: {:action=>"new", :controller=>"experiments"}, diff: {:quiz_id=>nil} (ActionController::RoutingError) 

我确信记录存在并且正确保存;如果我插入,

puts Quiz.find(1).title

我会得到“珍珠酱”。看起来 find_by 不起作用,这很奇怪,因为黄瓜生成的文件包含这个示例:

  #   when /^(.*)'s profile page$/i
  #     user_profile_path(User.find_by_login($1))     

我还尝试了一个更简单的条件查找,它也不起作用。

有什么想法吗?

预先感谢,
达维德

I am working on rails with rspec, rspec-rails, cucumber and webrat.
I am trying to learn BDD and testing in general.
I have a cucumber scenario like this:

  Scenario: Questions List
    Given quiz titled "Pearl Jam" has questions named "Corduroy, Dissident"
    When I go to the experiment page for quiz titled "Pearl Jam"
    Then I should see "Corduroy"
    And I should see "Dissident" 

I have alrady added step 1, where I create and save a quiz correctly (I tested through puts).
Now I am working on step 2. I am adding a new path in paths.rb

when /^the experiment page for quiz titled "(.*)"$/i 
  new_quiz_experiment_path(Quiz.find_by_title($1))  

For some reason find_by_title does not work. I get this error message:

new_quiz_experiment_url failed to generate from {:quiz_id=>nil, :action=>"new", :controller=>"experiments"}, expected: {:action=>"new", :controller=>"experiments"}, diff: {:quiz_id=>nil} (ActionController::RoutingError) 

I am sure the record is there, and correctly saved; if I insert

puts Quiz.find(1).title

I get "Pearl Jam". Looks like find_by is not working, which is quite weird since the cucumber generated file contains this example:

  #   when /^(.*)'s profile page$/i
  #     user_profile_path(User.find_by_login($1))     

I also tried a simpler find first with conditions, it doesn't work either.

Any idea?

Thanks in advance,
Davide

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

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

发布评论

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

评论(1

表情可笑 2024-08-09 12:15:40

感谢 ryanb 的建议,我能够找到该错误,该错误位于

Given quiz titled "Pearl Jam" has questions named "Corduroy, Dissident"

基本上我已经不一致地解析了此步骤;我已在第一个正则表达式中插入了引号,因此该图块被保存,因为

"Pearl Jam"

我想我已经学到了两件事:

  • 小心步骤定义中的引号,
  • 使用带有额外 ! 的方法来更具表现力错误消息。

Thanks to ryanb's suggestion I was able to find the bug, which was in

Given quiz titled "Pearl Jam" has questions named "Corduroy, Dissident"

Basically I had parsed this step inconsisetntly; I had inserted the quotation marks in the 1st regex, so the tile got saved as

"Pearl Jam"

I guess I've lerned two things:

  • Be careful with quotation marks in step definitions
  • use method with the extra ! for more expressive error messages.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文