Ruby on rails 黄瓜';给定';步骤通过,但相同';应该';步骤失败
我一直在学习SaaS课程,在第4章中我们应该使用Cucumber。我按照所有的说明观看了显示一切工作原理的屏幕广播,但我仍然被这个错误所困扰:Ruby on rails 黄瓜';给定';步骤通过,但相同';应该';步骤失败,ruby-on-rails,cucumber,Ruby On Rails,Cucumber,我一直在学习SaaS课程,在第4章中我们应该使用Cucumber。我按照所有的说明观看了显示一切工作原理的屏幕广播,但我仍然被这个错误所困扰: Scenario: Add a movie # features/AddMovie.feature:3 Given I am on the RottenPotatoes home page # features/step_definitions/web_steps.rb:
Scenario: Add a movie # features/AddMovie.feature:3
Given I am on the RottenPotatoes home page # features/step_definitions/web_steps.rb:44
When I follow "Add new movie" # features/step_definitions/web_steps.rb:56
Then I should be on the Create New Movie page # features/step_definitions/web_steps.rb:230
When I fill in "Title" with "Men In Black" # features/step_definitions/web_steps.rb:60
And I select "PG-13" from "Rating" # features/step_definitions/web_steps.rb:85
And I press "Save Changes" # features/step_definitions/web_steps.rb:52
Then I should be on the RottenPotatoes home page # features/step_definitions/web_steps.rb:230
<"/movies"> expected but was
<"/movies/1">. (MiniTest::Assertion)
./features/step_definitions/web_steps.rb:235:in `/^(?:|I )should be on (.+)$/'
features/AddMovie.feature:10:in `Then I should be on the RottenPotatoes home page'
And I should see "Men In Black" # features/step_definitions/web_steps.rb:105
Failing Scenarios:
cucumber features/AddMovie.feature:3 # Scenario: Add a movie
以及
path.rb
def path_to(page_name)
case page_name
when /^the home\s?page$/
'/'
when /^the RottenPotatoes home page/
movies_path
when /^the Create New Movie page/
new_movie_path
end
class MoviesController < ApplicationController
def index
@movies = Movie.all( :order => "title" )
end
def show
id = params[ :id ] # retrieve movie ID from URI route
begin
@movie = Movie.find( id )
rescue
flash[ :warning ] = "The movie was not found."
redirect_to movies_path
end
# will render app/views/movies/show.html.haml by default
end
def new
# default: render 'new' template
end
def create
@movie = Movie.create( params[ :movie ] )
flash[ :notice ] = "#{ @movie.title } was successfully created."
redirect_to movie_path( @movie.id )
end
def edit
id = params[ :id ]
@movie = Movie.find_by_id( id )
end
def update
@movie = Movie.find_by_id params[ :id ]
@movie.update_attributes!( params[ :movie ] )
flash[ :notice ] = "#{ @movie.title } was successfully updated."
redirect_to movie_path( @movie )
end
def destroy
@movie = Movie.find( params[ :id ] )
@movie.destroy
flash[ :notice ] = "Movie '#{ @movie.title }' deleted."
redirect_to movies_path
end
end
movies\u controller.rb
def path_to(page_name)
case page_name
when /^the home\s?page$/
'/'
when /^the RottenPotatoes home page/
movies_path
when /^the Create New Movie page/
new_movie_path
end
class MoviesController < ApplicationController
def index
@movies = Movie.all( :order => "title" )
end
def show
id = params[ :id ] # retrieve movie ID from URI route
begin
@movie = Movie.find( id )
rescue
flash[ :warning ] = "The movie was not found."
redirect_to movies_path
end
# will render app/views/movies/show.html.haml by default
end
def new
# default: render 'new' template
end
def create
@movie = Movie.create( params[ :movie ] )
flash[ :notice ] = "#{ @movie.title } was successfully created."
redirect_to movie_path( @movie.id )
end
def edit
id = params[ :id ]
@movie = Movie.find_by_id( id )
end
def update
@movie = Movie.find_by_id params[ :id ]
@movie.update_attributes!( params[ :movie ] )
flash[ :notice ] = "#{ @movie.title } was successfully updated."
redirect_to movie_path( @movie )
end
def destroy
@movie = Movie.find( params[ :id ] )
@movie.destroy
flash[ :notice ] = "Movie '#{ @movie.title }' deleted."
redirect_to movies_path
end
end
class MoviesController“标题”)
结束
def秀
id=params[:id]#从URI路由检索电影id
开始
@movie=movie.find(id)
营救
flash[:warning]=“找不到电影。”
重定向到电影路径
结束
#默认情况下,将呈现app/views/movies/show.html.haml
结束
def新
#默认值:呈现“新”模板
结束
def创建
@movie=movie.create(参数[:movie])
flash[:notice]=“#{@movie.title}已成功创建。”
重定向到电影路径(@movie.id)
结束
定义编辑
id=params[:id]
@movie=movie.find\u by\u id(id)
结束
def更新
@movie=movie.find_by_id参数[:id]
@movie.update_属性!(params[:movie])
flash[:notice]=“#{@movie.title}已成功更新。”
重定向到电影路径(@movie)
结束
def销毁
@movie=movie.find(参数[:id])
@电影,毁灭
flash[:notice]=“已删除电影“#{@Movie.title}”
重定向到电影路径
结束
结束
从您发布的内容判断,您的代码和测试似乎不一致。在测试中,您指定保存新电影后:
Then I should be on the RottenPotatoes home page
从path.rb文件判断,它看起来像是“应该在电影索引页上”。但是,在代码中,您不会在创建操作结束时重定向到电影索引页面,而是重定向到刚刚创建的电影的放映页面。这就解释了为什么Cumber抱怨它期望
(电影索引页)但得到了
(id=1的电影页面)
以下是您的创建操作:
def create
@movie = Movie.create( params[ :movie ] )
flash[ :notice ] = "#{ @movie.title } was successfully created."
redirect_to movie_path( @movie.id )
end
请参阅最后一行中的重定向到电影路径(@movie.id)
,它告诉rails将您发送到电影@movie
页面。这是有道理的,而且通常是您想要的(即,在用户创建新记录后,您会向他们显示他们创建的记录),但也有可能您希望将他们重定向到索引页,在那里他们可以看到他们与所有其他电影一起添加的电影。根据您想要的结果,解决方案将有所不同
假设测试正确,则需要将create
操作中的最后一行更改为:
redirect_to movies_path
希望有帮助 从您发布的内容来看,您的代码和测试似乎不一致。在测试中,您指定保存新电影后:
Then I should be on the RottenPotatoes home page
从path.rb文件判断,它看起来像是“应该在电影索引页上”。但是,在代码中,您不会在创建操作结束时重定向到电影索引页面,而是重定向到刚刚创建的电影的放映页面。这就解释了为什么Cumber抱怨它期望
(电影索引页)但得到了
(id=1的电影页面)
以下是您的创建操作:
def create
@movie = Movie.create( params[ :movie ] )
flash[ :notice ] = "#{ @movie.title } was successfully created."
redirect_to movie_path( @movie.id )
end
请参阅最后一行中的重定向到电影路径(@movie.id)
,它告诉rails将您发送到电影@movie
页面。这是有道理的,而且通常是您想要的(即,在用户创建新记录后,您会向他们显示他们创建的记录),但也有可能您希望将他们重定向到索引页,在那里他们可以看到他们与所有其他电影一起添加的电影。根据您想要的结果,解决方案将有所不同
假设测试正确,则需要将create
操作中的最后一行更改为:
redirect_to movies_path
希望有帮助 你能发布你的功能/步骤代码吗?如果不看到它,就无法调试它。还有它正在测试的任何代码。我刚刚编辑了我的帖子并添加了功能代码。感谢您的关注。这还不足以调试。你能包括你的电影控制器代码吗?我添加了电影控制器代码。一开始我不知道你也这么要求。对不起,你能发布你的功能/步骤代码吗?如果不看到它,就无法调试它。还有它正在测试的任何代码。我刚刚编辑了我的帖子并添加了功能代码。感谢您的关注。这还不足以调试。你能包括你的电影控制器代码吗?我添加了电影控制器代码。一开始我不知道你也这么要求。对不起,就是这个。非常感谢你,你帮了我很大的忙。就这样。非常感谢,你帮了我很大的忙。