RSpec未定义方法`request ='

时间:2015-04-24 16:45:18

标签: ruby-on-rails rspec

I am getting the following error from a RSpec get request:

 1) Flight GET/index displays flights
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `request=' for #<Flight:0x00000006a2ad68>

这是我的spec / requests / flights_spec.rb:

    require 'rails_helper'
    require 'spec_helper'
    require 'flight'


    RSpec.describe Flight, type: :controller  do
      describe 'GET/index', :type => :request do
       it 'displays flights' do
        Flight.create!(:destination => 'San Francisco')
        get :index
        response.body.should include('San Francisco')
       end
     end
    end

这是我的spec / controllers / flights_controller_spec.rb:

    require 'rails_helper'

    RSpec.describe FlightsController, type: :controller do
      describe 'GET index' do
      end
    end

这是我的app / controllers / flights_controller.rb:

    class FlightsController < ApplicationController
      before_action :set_flight, only: [:show, :edit, :update, :destroy]

      # GET /flights
      # GET /flights.json
      def index
        @flights = Flight.all
      end

      # GET /flights/1
      # GET /flights/1.json
      def show
  end

  # GET /flights/new
  def new
    @flight = Flight.new
  end

  # GET /flights/1/edit
  def edit
  end

  # POST /flights
  # POST /flights.json
  def create
    @flight = Flight.new(flight_params)

    respond_to do |format|
      if @flight.save
        format.html { redirect_to @flight, notice: 'Flight was successfully created.' }
        format.json { render :show, status: :created, location: @flight }
      else
        format.html { render :new }
        format.json { render json: @flight.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /flights/1
  # PATCH/PUT /flights/1.json
  def update
    respond_to do |format|
      if @flight.update(flight_params)
        format.html { redirect_to @flight, notice: 'Flight was successfully updated.' }
        format.json { render :show, status: :ok, location: @flight }
      else
        format.html { render :edit }
        format.json { render json: @flight.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /flights/1
  # DELETE /flights/1.json
  def destroy
    @flight.destroy
    respond_to do |format|
      format.html { redirect_to flights_url, notice: 'Flight was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_flight
      @flight = Flight.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def flight_params
      params.require(:flight).permit(:departure, :arrival, :destination, :baggage_allowance, :capacity)
    end
end

不确定是什么恭喜 - 任何帮助表示感谢 - 提前感谢, 斯拉夫科

1 个答案:

答案 0 :(得分:1)

在您的规范的顶部,您已经

RSpec.describe Flight, type: :controller

但接下来

describe "GET /index", type: :request

您必须选择其中一个(来自您可能需要控制器规格的规范中的内容)

除了控制器规范之外,所描述的对象应该是控制器类,而不是模型类。