gpt4 book ai didi

rspec - 带参数的用户让

转载 作者:行者123 更新时间:2023-12-02 09:13:39 24 4
gpt4 key购买 nike

我想测试这个类:

class Response

def initialize(raw_response:)
........
end
end

我想在 rspec 中发送多个 raw_responses。我实现了这段代码:

  let(:successful_response)    { File.read(File.join('spec', 'fixtures', 'xml', 'successful_response.xml')) }
......
let(:response) { described_class.new(raw_response: file_name) }

context '#response' do

it 'submits response' do

let(:file_name) { :successful_response }
expect(:response(raw_response: :file_name).parse_response).to include(................)
end
end

但是当我运行代码时出现这个错误:

syntax error, unexpected '(', expecting ')'
expect(:response(raw_response: :file_name).par...
^

我该如何解决这个问题?

最佳答案

这是你的做法:

context 'when response is successful' do
let(:file_name) { successful_response }
it 'submits response' do
expect(response.parse_response).to include(...)
end
end

context 'when response is unsuccessful' do
let(:file_name) { 'bad.xml' }
it 'submits response' do
expect(response.parse_response).to include(...)
end
end

不要在 it/specify 中使用 let - 它不会起作用。如果您需要更改不同上下文的值 - 使用 context

你可以把 let 想成定义一个内存方法。

let(:response) { described_class.new(raw_response: filename) }
let(:filename) { :foo }

就像

def response 
described_class.new(raw_response: filename)
end

def filename
:foo
end

关于rspec - 带参数的用户让,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49252070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com