我坚持在assert_template'预订/节目'在轨道上测试。

时间:2015-05-25 14:59:19

标签: ruby-on-rails

我正在开展一个简单的项目只是为了更深入地了解rails并且我遇到了导致此错误的简单测试assert_template 'bookings/show',我已多次检查,无法找到线索:

expecting <"bookings/show"> but rendering with <[]>
            test/integration/booking_submit_test.rb:17:

相关代码如下:

测试/集成/ booking_submit_test.rb

require 'test_helper'

class BookingSubmitTest < ActionDispatch::IntegrationTest
     test "invalid booking submit information" do
        get booknow_path
        assert_no_difference 'Booking.count' do
          post bookings_path, booking: {date_of_tour: "2017-05-06", hotel_name: "xxx hotel", phone_number:123456 , number_of_pax:34 , pick_up_time: "9:00"} 
     end
     assert_template 'bookings/new'
    end

    test "valid booking submit information" do
        get booknow_path
        assert_difference 'Booking.count', 1 do
            post bookings_path, booking: {date_of_tour: "2017-05-06", hotel_name: "xxx hotel", phone_number:12345678901 , number_of_pax:34 , pick_up_time: "9:00"}
        end
        assert_template 'bookings/show'
    end
end

show.html.rb:

<% provide(:title, @booking.date_of_tour) %>
<div class="row">
 <aside class="col-md-4">
    <section class="user_info">
<h1>
<%= @booking.hotel_name %>
     </h1>
   </section>
  </aside>
</div>

new.html.rb:

<% provide(:title, 'Book the package now') %>
<h1>Book now</h1>
<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@booking) do |f| %>
    <%= render 'shared/errorbooking_messages' %>

      <%= f.label :date_of_tour %>
      <%= f.text_field :date_of_tour, class: 'form-control' %>

      <%= f.label :hotel_name %>
      <%= f.text_field :hotel_name, class: 'form-control' %>

      <%= f.label :phone_number %>
      <%= f.text_field :phone_number, class: 'form-control' %>

      <%= f.label :number_of_pax %>
      <%= f.text_field :number_of_pax, class: 'form-control' %> 

      <%= f.label :pick_up_time %>
      <%= f.text_field :pick_up_time, class: 'form-control' %>

      <%= f.submit "Book now", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

bookings_controller.rb:

class BookingsController < ApplicationController
  def show
    @booking = Booking.find(params[:id])
  end

  def new
    @booking = Booking.new
  end

  def create
    @booking = Booking.new(booking_params)    # Not the final implementation!
    if @booking.save
      flash[:success] = "You have submited the information successfully!"
      redirect_to @booking
    else
      render 'new'
    end

  def edit
    @booking = Booking.find(params[:id])
  end
end


  private
  def booking_params
    params.require(:booking).permit(:date_of_tour, :hotel_name, :phone_number, :number_of_pax, :pick_up_time )
  end
end

的routes.rb

get    'booknow'   => 'bookings#new'
  resources :bookings

1 个答案:

答案 0 :(得分:1)

当输入正确时,操作会执行redirect_to,因此您应该使用assert_redirected_to进行测试:

test "valid booking submit information" do
    get booknow_path
    assert_difference 'Booking.count', 1 do
        post bookings_path, booking: {date_of_tour: "2017-05-06", hotel_name: "xxx hotel", phone_number:12345678901 , number_of_pax:34 , pick_up_time: "9:00"}
    end
    assert_redirected_to booking_path(assigns(:booking))
end