2017-08-28 95 views
0

我是Rails的新手,特别是写测试。当我运行测试时,我不断收到错误undefined method allowundefined method allow_any_instance_of'。 我检查了Rspec> 3应该接受允许的语法,我的代码中有什么问题?未定义的方法`允许'为#<RSpec :: Rspec 3.6.0

describe "GET #show" do 

    let(:sample_json_list) { 
     "{\"files\":[{\"title\":\"discourse-2017-08-24-085545-v20170803123704.sql.gz\",\"id\":\"0B7WjYjWZJv_4MENlYUM2SjkyU1E\",\"size\":\"14070939\",\"created_at\":\"2017-08-24T06:56:21.698+00:00\"}]}" 
    } 

    before { 
     allow_any_instance_of(DiscourseDownloadFromDrive::DriveDownloader).to receive(:json_list).and_return(sample_json_list) 
    } 

    it "renders the json" do 
     get :show 
     response.body.should == sample_json_list 
    end 

    end 
+0

你可以用'-b'选项运行你的规范并显示更多日志吗? – Tai

回答

1

好的,首先配置expect语法。会是这样的:

# spec_helper.rb 
RSpec.configure do |config| 
    config.expect_with :rspec do |c| 
    c.syntax = :expect 
    end 
end 

现在allow_any_instance_of会像魅力。 但是,那么语法会看起来像:

it "renders the json" do 
    get :show 
    expect(response.body).to eq(sample_json_list) 
end 

干杯!

+0

啊!我明白了,所以如果项目有'RSpec.configure do | config | config.fail_fast = ENV [ 'RSPEC_FAIL_FAST'] == “1” config.include助手 config.include MessageBus config.include RSpecHtmlMatchers config.include IntegrationHelpers,类型:请求 config.mock_framework =:摩卡 配置.order ='random' config.infer_spec_type_from_file_location!' 这意味着使用摩卡语法代替rspec期望吗? – catch22

+1

我很害怕。默认情况下'mock_framework'是':rspec'。所以也许只是删除'config.mock_framework =:mocha'也可以。 :) – rony36

相关问题