【发布时间】:2016-07-11 21:48:05
【问题描述】:
我已经被这个卡住了一段时间,我一直在搜索,但我无法弄清楚为什么我的代码不起作用......
我正在学习 Cucumber,我必须填充数据库来运行场景。
这些是说明:
(...) 您将创建一个与步骤匹配的步骤定义
Given the following movies exist在两者的Background部分sort_movie_list.feature和filter_movie_list.feature。 (后来在 在课程中,我们将展示如何干燥重复的Background两个功能文件中的部分。)在
movie_steps.rb步骤定义文件中添加您的代码。你可以 只需使用 ActiveRecord 调用直接将电影添加到数据库; 绕过与创建新电影相关的 GUI 是可以的,因为 这不是这些场景正在测试的内容。
这是*.feature 文件之一
功能:显示按 MPAA 分级过滤的电影列表
As a concerned parent
So that I can quickly browse movies appropriate for my family
I want to see movies matching only certain MPAA ratings
Background: movies have been added to database
Given the following movies exist:
| title | rating | release_date |
| Aladdin | G | 25-Nov-1992 |
| The Terminator | R | 26-Oct-1984 |
| When Harry Met Sally | R | 21-Jul-1989 |
| The Help | PG-13 | 10-Aug-2011 |
| Chocolat | PG-13 | 5-Jan-2001 |
| Amelie | R | 25-Apr-2001 |
| 2001: A Space Odyssey | G | 6-Apr-1968 |
| The Incredibles | PG | 5-Nov-2004 |
| Raiders of the Lost Ark | PG | 12-Jun-1981 |
| Chicken Run | G | 21-Jun-2000 |
这是我来自*_steps.rb的代码:
Given /the following movies exist/ do |movies_table|
movies_table.hashes.each do |movie|
Movie.create!(movie)
end
fail "Unimplemented"
end
这是我得到的错误:
Background: movies have been added to database # features/sort_movie_list.feature:7
Given the following movies exist: # features/step_definitions/movie_steps.rb:3
| title | rating | release_date |
| Aladdin | G | 25-Nov-1992 |
| The Terminator | R | 26-Oct-1984 |
| When Harry Met Sally | R | 21-Jul-1989 |
| The Help | PG-13 | 10-Aug-2011 |
| Chocolat | PG-13 | 5-Jan-2001 |
| Amelie | R | 25-Apr-2001 |
| 2001: A Space Odyssey | G | 6-Apr-1968 |
| The Incredibles | PG | 5-Nov-2004 |
| Raiders of the Lost Ark | PG | 12-Jun-1981 |
| Chicken Run | G | 21-Jun-2000 |
Unimplemented (RuntimeError)
./features/step_definitions/movie_steps.rb:7:in `/the following movies exist/'
features/sort_movie_list.feature:9:in `Given the following movies exist:'
我试过movie = Movie.create!, Movie.create!(movie), Movie.create! movie, movie = Movie.create!(这最后一个只是为了纯粹的绝望)......我做错了什么?
提前致谢:)
【问题讨论】:
-
我的钱在冒号 (
:) 上,您在正则表达式中错过了。 -
什么正则表达式?
movie是一个代表哈希的变量 -
你有步骤“Given the following movies exist:”,然后你尝试将它与“Given /the following movies exist/do |movies_table|”匹配。您用来匹配步骤定义的正则表达式中的
:在哪里? -
是否需要正则表达式?这些语法已经在练习中了,我应该只写 |movie| 之后的块...而且我刚刚检查了 Cucumber 文档,我看到了带有表格的示例,没有正则表达式 github.com/cucumber/cucumber/wiki/…
-
更正:“是否需要
:正则表达式?
标签: ruby-on-rails cucumber scenarios