我正在使用 double/stub 编写测试,我想返回一个假值,但我得到的是假值和真实值。这是我的控制器和规格文件,模型中没有任何内容。任何想法我做错了什么?我的猜测是我做错了。Ps 我对规范和模拟/存根的东西有点陌生
控制器:
def destroy
uri = URI('example.com')
request_params = {'requestId' => @session.id,
'merchantId' => MERCHANTID,
'locale' => LOCALE,
'partnerId' => PARTNERID,
'sessionId' => params[:id]
}
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
request = Net::HTTP::Post.new uri.request_uri
request.set_form_data(request_params)
#request.content_type = 'application/x-www-form-urlencoded'
response = http.request request # Net::HTTPResponse object
json = ActiveSupport::JSON.decode(response.body)
puts '<<<<<<<<<'
puts json
if json['expireSessionRes']['isError'] == false
render :json => {:status => 'ok', :message => 'logged out'}
else
render :json => {:status => 'failed', :message => 'logout failed'}
end
end
规格文件:
it "should correctly return isError false if logout was sucessful" do
SID = "123"
@data3 = {:id => SID}
delete :destroy, default_rest_params.merge(@data3)
response = double('http.request')
response.stub!(:body => '{"expireSessionRes"=>{"requestId"=>"72", "sessionId"=>"123", "isError"=>false, "desc"=>""}}')
puts response.body
end
接收:
{"expireSessionRes"=>{"requestId"=>"94", "isError"=>true, "desc"=>"Session exipred", "errorCode"=>"-10011"}}
{"expireSessionRes"=>{"requestId"=>"72", "sessionId"=>"123", "isError"=>false, "desc"=>""}}
第一个来自控制器第二个来自存根