【发布时间】:2016-06-04 23:22:00
【问题描述】:
我尝试为 Rails API 中的“显示”操作编写一些测试
require 'rails_helper'
RSpec.describe AirlinesController, type: :controller do
describe "GET #show" do
before(:each) do
@airline = FactoryGirl.create(:airline)
get :show, id: @airline.id
end
it "should return the airline information" do
airline_response = json_response
expect(airline_response[:name]).to eql @airline.name
end
it {should respond_with :ok}
end
end
测试通过了。但是,当我尝试像这样使用let 和subject 时
require 'rails_helper'
RSpec.describe AirlinesController, type: :controller do
describe "GET #show" do
let(:airline) {FactoryGirl.create(:airline)}
subject {airline}
before(:each) do
get :show, id: airline.id
end
it "should return the airline information" do
airline_response = json_response
expect(airline_response[:name]).to eql airline.name
end
it {should respond_with :ok}
end
end
它显示“NoMethodError undefined method `response' for ...”
这让我很困惑!
【问题讨论】:
标签: ruby-on-rails ruby rspec rspec-rails