我的模型中有一个下面的方法,我想要存根下面的值
" esxi_hosts = RezApi.new.get_esxi(type, vrealm_id)
"
的值为[{x: "y"}]
在rspec中执行此操作的方法是什么。
def create_esxi
if (["vRealm", "Praxis Parent vRealm", "Praxis Child vRealm"].include?(self.collection_type.try(:name)))
esxi_hosts = []
if(( self.parent && self.parent.parent && self.parent.collection_type.name.upcase == "POD" && self.parent.parent.collection_type.name.upcase == "DATACENTER") or ( self.parent && self.parent.parent && self.parent.parent.parent && (self.parent.collection_type.name.upcase == "RELEASE - PRAXIS" or self.parent.collection_type.name.upcase == "RELEASE - SUBSCRIPTION") && self.parent.parent.collection_type.name.upcase == "POD" && self.parent.parent.parent.collection_type.name.upcase == "DATACENTER"))
vrealm_type = collection_type.try(:name)
vrealm_id = "d#{self.parent.parent.instance}p#{self.parent.instance}v#{instance}"
case vrealm_type
when "vRealm"
types = ["vrealm-multitenant-dr2c", "vpc-standard"]
types.each do |type|
if esxi_hosts.empty?
esxi_hosts = RezApi.new.get_esxi(type, vrealm_id)
end
end
when "Praxis Parent vRealm"
esxi_hosts = RezApi.new.get_esxi("praxis-core", vrealm_id)
when "Praxis Child vRealm"
esxi_hosts = RezApi.new.get_esxi("praxis-node-mgmt", vrealm_id)
end
end
if esxi_hosts.flatten.any?
assign_esxi(esxi_hosts)
end
end
end
我已经尝试过以下代码,但它没有用
require 'spec_helper'
describe "Esxi Host creation from Rez Api" do
let(:federation) {create(:federation_collection, parent_id: nil, name: "Test", usage: "Tech Ops hosted in Vmware Data Centers", owner: "--", date_from: "2015-08-26", date_to: nil, collection_type_id: 2, zone_name: "se.vpc.vmw")}
let(:datacenter) {create(:datacenter_collection, name: "Datacenter", parent_id: federation.id)}
let(:pod) {create(:pod_collection, parent_id: datacenter.id)}
let(:fqdn) {"d2p1s0ch10srv0v101-esx0.se.vpc.vmw"}
let(:vrealm1) {create(:vrealm_collection, name: "vRealm1", parent_id: pod.id)}
let(:vrealm2) {create(:vrealm_collection, name: "vRealm2", parent_id: pod.id)}
context "When response has esxi hosts " do
let(:rez_response) {[{"esx0"=>{"nodefqdn"=>"d2p1s0ch10srv0.se.vpc.vmw","fqdn"=>"d2p1s0ch10srv0v101-esx0.se.vpc.vmw","vmk0"=>{"pg_name"=>"d2p1v101-esx-pg-1062","ip_addr"=>"10.141.112.71","netmask"=>"255.255.255.0",},"vmk1"=>{"pg_name"=>"d2p1pod-sto-pg-17","ip_addr"=>"172.16.160.51","netmask"=>"255.255.252.0"},"vmk2"=>{"pg_name"=>"d2p1v101-ftx-pg-1063","ip_addr"=>"172.16.165.126","netmask"=>"255.255.255.0"}}}]}
subject {RezApi.new}
it "should create esxi hosts" do
allow(subject).to receive(:get_esxi).with("type", "101").and_return(rez_response)
expect(subject).to receive(:get_esxi).with("type", "101").and_return(rez_response)
vrealm1.create_esxi
vrealm1.resources.map(&:fqdn).should include(fqdn)
end
end
end
获得以下错误
Failures:
1) Esxi Host creation from Rez Api When response has esxi hosts should create esxi hosts
Failure/Error: expect(subject).to receive(:get_esxi).with("type", "101").and_return(rez_response)
(#<RezApi:0xc26d69c>).get_esxi("type", "101")
expected: 1 time with arguments: ("type", "101")
received: 0 times
答案 0 :(得分:0)
subject { RezApi.new }
context "....." do
it "....." do
allow(subject).to receive(:get_esxi).with("type", "101").and_return({x: "y"})
expect(subject).to receive(:get_esxi).with("type", "101").and_return({x: "y"})
end
end
因此,您更新的测试代码应为:
describe "Esxi Host creation from Rez Api" do
let(:federation) {create(:federation_collection, parent_id: nil, name: "Test", usage: "Tech Ops hosted in Vmware Data Centers", owner: "--", date_from: "2015-08-26", date_to: nil, collection_type_id: 2, zone_name: "se.vpc.vmw")}
let(:datacenter) {create(:datacenter_collection, name: "Datacenter", parent_id: federation.id)}
let(:pod) {create(:pod_collection, parent_id: datacenter.id)}
let(:fqdn) {"d2p1s0ch10srv0v101-esx0.se.vpc.vmw"}
let(:vrealm1) {create(:vrealm_collection, name: "vRealm1", parent_id: pod.id)}
let(:vrealm2) {create(:vrealm_collection, name: "vRealm2", parent_id: pod.id)}
context "When response has esxi hosts " do
let(:rez_response) {[{"esx0"=>{"nodefqdn"=>"d2p1s0ch10srv0.se.vpc.vmw","fqdn"=>"d2p1s0ch10srv0v101-esx0.se.vpc.vmw","vmk0"=>{"pg_name"=>"d2p1v101-esx-pg-1062","ip_addr"=>"10.141.112.71","netmask"=>"255.255.255.0",},"vmk1"=>{"pg_name"=>"d2p1pod-sto-pg-17","ip_addr"=>"172.16.160.51","netmask"=>"255.255.252.0"},"vmk2"=>{"pg_name"=>"d2p1v101-ftx-pg-1063","ip_addr"=>"172.16.165.126","netmask"=>"255.255.255.0"}}}}
subject { RezApi.new }
it "should create esxi hosts" do
allow(subject).to receive(:get_esxi).with("vrealm-multitenant-dr2c", "101").and_return(rez_response)
expect(subject).to receive(:get_esxi).with("vrealm-multitenant-dr2c", "101").and_return(rez_response)
vrealm1.create_esxi
vrealm1.resources.map(&:fqdn).should include(fqdn)
end
end
end
答案 1 :(得分:0)
使用以下代码
RezApi.any_instance.stub(:get_esxi).with("vrealm-multitenant-dr2c", "101").and_return(success_response)