集成测试在rails 4中获取ActionController :: UrlGenerationError

时间:2015-05-30 16:44:00

标签: ruby-on-rails integration-testing

我是铁杆新手:( 试着编写一些测试来编辑用户的预订信息,这里是我得到的,我已经检查了stackover上的其他帖子,它似乎无关任何线索?对你的帮助表示感谢 !

require 'test_helper'
    class BookingEditTest < ActionDispatch::IntegrationTest
        def setup
            @user = users(:michael)
        end

        test "unsucessful edit" do 
          log_in_as(@user)
          get edit_booking_path(@booking)
          assert_template 'bookings/edit'
          patch booking_path(@booking), booking: {date_of_tour: "2017-05-06", hotel_name: " ", hotel_address:"dadsada das",phone_number:12345678901 , number_of_pax:34 , pick_up_time: "9:00"}
          assert_template 'bookings/edit'
        end
    end

测试代码:

resources :bookings, only: [:show,:new, :create, :edit, :update,:destroy]

路线:class BookingsController < ApplicationController before_action :logged_in_user, only: [:show, :edit,:create, :destroy] def show @booking = Booking.find(params[:id]) end def new @booking = Booking.new end def create @booking = current_user.bookings.build(booking_params) if @booking.save flash[:success] = "You have submited the information successfully!" redirect_to root_url else render 'new' end end def edit @booking = Booking.find(params[:id]) end def update @booking = current_user.bookings.find_by(params[:id]) if @booking.update_attributes(booking_params) flash[:success] = "information updated" redirect_to @booking else render 'edit' end end def destroy end private def booking_params params.require(:booking).permit(:date_of_tour,:hotel_address,:hotel_name,:phone_number,:number_of_pax,:pick_up_time) end end

预订模式: belongs_to:用户

用户模型: has_many:预订,依赖:: destroy

booking_controller.rb

<table data-toggle="table" data-url="data" data-cache="false" data-height="299">
    <thead>
        <tr>
            <th data-field="id">Item ID</th>
            <th data-field="name">Name</th>
        </tr>
    </thead>
</table>

1 个答案:

答案 0 :(得分:2)

你在说

edit_booking_path(@booking)

但是@booking尚未定义,路径助手无法为不存在的预订生成网址。

在尝试使用之前,您必须创建一个Booking实例。