我正在开发Plain Ruby项目(非Rails环境(。然而,我得到了一个错误,
#<Double "pr"> received unexpected message :client with (no args)
此错误是从标签为pr的double对象返回的。这是我的rspec,我已经将Sinatra配置为假github服务器,并返回JSON响应。我已经验证了结果及其返回的JSON响应。
RSpec.describe Humdrum::DefaultVerifications::CodeReviewsSignedOff do
describe '.Code Reviews Signed Off' do
let(:org){
'octocat'
}
let(:number){
123
}
before do
uri = URI('https://api.github.com/repos/octocat/hello-world/pulls/42/reviews')
result=JSON.load(Net::HTTP.get(uri))
github=double
allow(github)
.to receive_message_chain(:client,:pull_request_reviews)
.with(:org,:number)
.and_return (result)
end
it 'should check pull request review response' do
object=Humdrum::DefaultVerifications::CodeReviewsSignedOff.new
github=double("pr")
expert(object.pull_request_reviews_response(github)). to eq(1)
end
end
end
正如您在函数pull_request_reviews_response中所看到的,我想将github.client.pull_request_reviews截尾,因此,在该文件的rspec中,我写了allow,消息链,从那里它返回json响应。json响应将在同一函数中进行处理,并返回一个整数响应
module Humdrum
module DefaultVerifications
class CodeReviewsSignedOff
def pull_request_reviews_response(github)
#Counting total number of user who have approved the pull request
approveCount=0
github.client.pull_request_reviews("#{github.organization}/#{github.repository}", github.number).each do |review|
username = review[:user][:login]
state = review[:state]
if state == "APPROVED" and !@@approvedUser.include?(username)
@@approvedUser.add(username)
puts "Changes #{state} by #{username}"
approveCount += 1
end
end
return approveCount
end
我做错了什么?
我做错了什么?
您已经在两个不同的地方将github
定义为局部变量:
before
# ...
github = double # <----- HERE
allow(github)
.to receive_message_chain(:client, :pull_request_reviews)
.with(:org, :number)
.and_return(result)
end
it 'should check pull request review response' do
# ...
github = double("pr") # <-- AND ALSO HERE
expert(object.pull_request_reviews_response(github)).to eq(1)
end
因此,您发送给该方法的对象没有任何存根。因此出现了错误消息。
你可以选择多种方式来构建这个测试(例如,我们可以讨论使用double
通常是个坏主意,使用receive_message_chain
也是如此!……我会选择至少使用instance_double
,甚至可能只是在这里传递一个真实的对象。(
但作为一个最小的更改,这里有一种方法可以定义github
变量一次,并在before
块和规范本身中引用相同的对象:
let(:github) { double("pr") } # <---- !!!
before
# ...
allow(github)
.to receive_message_chain(:client, :pull_request_reviews)
.with(:org, :number)
.and_return(result)
end
it 'should check pull request review response' do
# ...
expert(object.pull_request_reviews_response(github)).to eq(1)
end