问题描述
我正在尝试让这些规范在一个项目中发挥作用。有规格设置,但未进行维护。
我有一篇关于request spec
require 'rails_helper'
RSpec.describe 'Cars API', type: :request do
let(:organization) { Fabricate(:organization, owner: user) }
let(:app) { Fabricate(:app, organization: organization, owner: user) }
let(:user) { Fabricate(:user) }
before do
#login_api(user)
end
describe 'create' do
describe 'car type' do
it 'should create a car of type CarType1' do
expect(Car.count).to eql(0)
id = '123'
post("/api/1/organization/#{id}/cars")
expect(Car.count).to eql(1)
car = Car.first
expect(car.type).to eql(Car::CarType1)
end
end
end
end
我得到
#<NoMethodError: undefined method `call' for #<App:0x007fee64557080>>
我已尝试调试该问题,但没有成功。
问题发生在第post("/api/1/organization/#{id}/cars")
行。
问题可能出在哪里?
推荐答案
我遇到了这样的问题;)(我花了几个小时调试源代码)
尝试将app
重命名为ap
或类似的变量。
# "bad" name
let(:app) { ... }
# "better" name
let(:ap) { ... }
# or
let(:my_app) { ... }
正如我所理解的,当RSpec初始化您的惰性块let
时,变量名中的问题。不确定,但我认为此时App
已经初始化,RSpec将方法call
发送到另一个对象,而不是从let
阻止。
注意:可以使用BEFORE BLOCK和使用实例变量@app