搜索电影的用户故事features/AddTMDbSearch.feature
Feature: User can add movie by searching for it in The Movie Database (TMDb)
As a movie fan
So that I can add new movies without manual tedium
I want to add movies by looking up their details in TMDb
Scenario: Try to add nonexistent movie (sad path)
Given I am on the RottenPotatoes home page
Then I should see "Search TMDb for a movie"
When I fill in "Search Terms" with "Movie That Does Not Exist"
And I press "Search TMDb"
Then I should be on the RottenPotatoes home page
And I should see "'Movie That Does Not Exist' was not found in TMDb."
可以试着运行
cucumber features/AddTMDbSearch.feature
为了使得测试通过,我们修改app/views/movies/index.html.haml,在末尾添加
%h1 Search TMDb for a movie
= form_tag :action=>:search_tmdb do
= label :search,:terms, 'Search Terms'
= text_field_tag :search_terms
= submit_tag 'Search TMDb'
既然添加了action,必须修改路由config/routes.rb
post 'movies/search_tmdb',action: :search_tmdb,controller: :movies
同时在控制器中添加操作到app/controllers/movies_controller.rb
def search_tmdb
# hardwire to simulate failure
flash[:warning] = "'#{params[:search_terms]}' was not found in TMDb."
redirect_to movies_path
end
再次运行
cucumber features/AddTMDbSearch.feature
我们思考以下场景,用户搜索一个已存在的电影会怎样,这时候用户首先打开页面,确保存在一个搜索框,这两个步骤和失败路径一摸一样。因此我们想这样重复的步骤是否需要精简(敏捷的要诀,去掉重复内容)。解决方案是:引入Background部分,修改features/AddTMDbSearch.feature成如下:
Feature: User can add movie by searching for it in The Movie Database (TMDb)
As a movie fan
So that I can add new movies without manual tedium
I want to add movies by looking up their details in TMDb
Background: Start from the Search form on the home page
Given I am on the RottenPotatoes home page
Then I should see "Search TMDb for a movie"
Scenario: Try to add nonexistent movie (sad path)
When I fill in "Search Terms" with "Movie That Does Not Exist"
And I press "Search TMDb"
Then I should be on the RottenPotatoes home page
And I should see "'Movie That Does Not Exist' was not found in TMDb."
Scenario: Try to add existing movie (happy path)
When I fill in "Search Terms" with "Inception"
And I press "Search TMDb"
Then I should be on the "Search Results" page
And I should not see "not found"
And I should see "Inception"