简体   繁体   English

无法使请求的规范生效:#的未定义方法“调用”

[英]Cannot make rails spec for a request working : `undefined method 'call' for #<App:`

I'm trying make the specs work on a project. 我正在尝试使规格适用于一个项目。 There was a setup for specs but was not maintain. 有用于规格的设置,但未维护。

I have a simple post for a request spec 我有一个简单的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

And I get 我得到

#<NoMethodError: undefined method `call' for #<App:0x007fee64557080>>

I've tried to debug the issue but with no luck. 我试图调试问题,但没有运气。 The issue happens at the line post("/api/1/organization/#{id}/cars") . 该问题发生在line post("/api/1/organization/#{id}/cars") Where might the problem be ? 问题可能在哪里?

I had such problem ;) (and spent several hours for debugging sources) 我遇到了这样的问题;)(花了几个小时调试源代码)

Try to rename app to ap or similar variable. 尝试将app重命名为ap或类似变量。

# "bad" name
let(:app)          { ... }

# "better" name
let(:ap)          { ... }
# or 
let(:my_app)      { ... }

As I understood the problem in variable name when RSpec initializes your lazy block let . 据我了解,RSpec初始化您的惰性块let时,变量名中存在问题。 Not sure but I think at that moment App has already initialized and RSpec send method call not to block from let but to another object. 不确定,但我认为那时App已经初始化,RSpec send方法call不是阻止let而是阻止另一个对象。

Note: you can use before block and use instance variable @app 注意:您可以使用before块并使用实例变量@app

This issue is occuring because you are overriding the Rails app variable. 发生此问题是因为您要覆盖Rails app变量。 Rename the variable to something else. 将变量重命名为其他名称。 It will work. 它会工作。

By default in rails, app variable is your Rails application which has call method that will be invoked whenever your application receives any request from Rack middleware. 在Rails中,默认情况下, app变量是您的Rails应用程序,该应用程序具有call方法,只要您的应用程序收到来自Rack中间件的任何请求,该方法便会被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM