2015-04-24 97 views
1
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> 

這是我的規格/請求/ flights_spec.rb:RSpec的未定義的方法`請求=」

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 

這是我的規格/控制器/ flights_controller_spec.rb:

require 'rails_helper' 

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

這裏是我的應用程序/ 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 

不知道發生了什麼鑼上 - 任何幫助讚賞 - 在此先感謝, 斯拉夫科

+0

您可能在此處未顯示代碼中的某處出現'@flight.request ='。也許在一個視圖? – infused

回答

1

在你規範的頂部,你已經有了

RSpec.describe Flight, type: :controller 

但隨後略低於

describe "GET /index", type: :request 

你必須選擇其中一種(從規格中的內容來看,您可能需要控制器規格)

另外,對於控制器規格,所描述的對象應該是一個控制器類,而不是一個模型類。

+0

ok - 將'request'更改爲'controller',並將創建模型實例行註釋掉,但仍然出現相同的錯誤。 – user3570192

+0

@ user3570192你還有'描述飛行' –

相關問題