1

我一直在学习 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: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

Feature: User can manually add movie

Scenario: Add a movie
    Given I am on the RottenPotatoes home page
    When I follow "Add new movie"
    Then I should be on the Create New Movie page
    When I fill in "Title" with "Men In Black"
    And I select "PG-13" from "Rating"
    And I press "Save Changes"
    Then I should be on the RottenPotatoes home page
    And I should see "Men In Black"

和代码片段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

movies_controller.rb

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
4

1 回答 1

1

从您发布的内容来看,您的代码和测试似乎不一致。在您的测试中,您指定保存新电影后:

Then I should be on the RottenPotatoes home page

从您的 path.rb 文件来看,它看起来像是“应该在电影索引页面上”。但是,在您的代码中,您不会在创建操作结束时重定向到电影索引页面,而是重定向到刚刚创建的电影的放映页面。这可以解释为什么黄瓜抱怨它期望<"/movies">(电影索引页面)但得到<"/movies/1">(电影页面id = 1)。

这是您的创建操作:

def create
  @movie = Movie.create( params[ :movie ] )
  flash[ :notice ] = "#{ @movie.title } was successfully created."
  redirect_to movie_path( @movie.id )
end

请参阅您拥有的最后一行redirect_to movie_path(@movie.id),它告诉 rails 将您发送到该电影的页面@movie。这是有道理的并且通常是您想要的(即在用户创建新记录后,您向他们展示他们创建的记录),但您也可能希望将他们重定向到他们可以看到他们添加的电影的索引页面与所有其他电影。解决方案将根据您想要的结果而有所不同。

假设您的测试是正确的,您需要将create操作中的最后一行更改为:

redirect_to movies_path

希望有帮助!

于 2012-09-15T22:17:48.307 回答