0

我在 Edx.org 的 SaaS 的 HW4 工作。我在场景 2 和 3 中运行 cucumber 时遇到问题。当我尝试使用 Web 应用程序时,它在这两个场景中运行良好。但是运行黄瓜,我得到这个输出:

Scenario: find movie with same director                       #        features/search_movies_director.feature:22
Deprecated: please use #source_tags instead.
Given I am on the details page for "Star Wars"              # features/step_definitions/web_steps.rb:44
When I follow "Find Movies With Same Director"              # features/step_definitions/web_steps.rb:56
Then I should be on the Similar Movies page for "Star Wars" # features/step_definitions/web_steps.rb:230
  No route matches {:action=>"director", :controller=>"movies"} (ActionController::RoutingError)
  ./features/support/paths.rb:20:in `path_to'
  ./features/step_definitions/web_steps.rb:233:in `/^(?:|I )should be on (.+)$/'
  features/search_movies_director.feature:25:in `Then I should be on the Similar Movies page for "Star Wars"'
And I should see "THX-1138"                                 # features/step_definitions/web_steps.rb:105
But I should not see "Blade Runner"                         # features/step_definitions/web_steps.rb:123

Scenario: can't find similar movies if we don't know director (sad path) # features/search_movies_director.feature:29
Deprecated: please use #source_tags instead.
Given I am on the details page for "Alien"                             # features/step_definitions/web_steps.rb:44
Then I should not see "Ridley Scott"                                   # features/step_definitions/web_steps.rb:123
When I follow "Find Movies With Same Director"                         # features/step_definitions/web_steps.rb:56
Then I should be on the home page                                      # features/step_definitions/web_steps.rb:230
  expected: "/movies"
       got: "/movies/3/director" (using ==) (RSpec::Expectations::ExpectationNotMetError)
  ./features/step_definitions/web_steps.rb:233:in `/^(?:|I )should be on (.+)$/'
  features/search_movies_director.feature:33:in `Then I should be on the home page'
And I should see "'Alien' has no director info"                        # features/step_definitions/web_steps.rb:105

在这两种情况下,当我手动执行此方案时,行为都是正确的。问题是什么?

谢谢

当我执行“rake routes”时,我得到:

director_movie GET    /movies/:id/director(.:format) {:action=>"director", :controller=>"movies"}
    movies GET    /movies(.:format)              {:action=>"index", :controller=>"movies"}
           POST   /movies(.:format)              {:action=>"create", :controller=>"movies"}
 new_movie GET    /movies/new(.:format)          {:action=>"new", :controller=>"movies"}
edit_movie GET    /movies/:id/edit(.:format)     {:action=>"edit", :controller=>"movies"}
     movie GET    /movies/:id(.:format)          {:action=>"show", :controller=>"movies"}
           PUT    /movies/:id(.:format)          {:action=>"update", :controller=>"movies"}
           DELETE /movies/:id(.:format)          {:action=>"destroy", :controller=>"movies"}

在这里,我可以看到路由“director_movie GET /movies/:id/director(.:format) {:action=>"director", :controller=>"movies"}",所以我不明白为什么第一个场景失败。

在第二个中,它期望“/movies”并得到“/movies/3/director”。这是来自控制器的一段代码:

def director
 mov = Movie.find(params[:id])
 dir = mov.director
 if (dir == nil)
   flash[:notice] = "'#{mov.title}' has no director info"
   redirect_to movies_path
 else
   @movies = Movie.find_all_by_director(dir)
 end
end

但是当我手动模拟相同的步骤时,我看到了 flash 消息并重定向到“/movies”......为什么黄瓜不一样?

对不起我的英语......谢谢!

4

1 回答 1

1

问题是你没有检查导演也可能是空的。在这种情况下,不会重定向到主页,并且场景失败。为了解决这种情况,您需要检查 nil 或空:

 if (dir.nil? || dir.empty?)
   flash[:notice] = "'#{mov.title}' has no director info"
   redirect_to movies_path
 else
   @movies = Movie.find_all_by_director(dir)
 end
于 2013-02-16T14:33:55.367 回答