如何在RSpec中创建外部ruby gem的存根方法?
我试图在多个操作系统中测试类方法的行为,而不需要在它们上运行代码。我在用电话 我想在RSpec中测试此函数的3种情况:如何在RSpec中创建外部ruby gem的存根方法?,ruby,rspec2,Ruby,Rspec2,我试图在多个操作系统中测试类方法的行为,而不需要在它们上运行代码。我在用电话 我想在RSpec中测试此函数的3种情况: def get_os() if OS.linux? return "linux#{OS.bits}" elsif OS.mac? return "mac" elsif OS.doze? return "win" end 我已经创建了一个快速测试项目 您可以通过以下方式运行此操作: git clone git:
def get_os()
if OS.linux?
return "linux#{OS.bits}"
elsif OS.mac?
return "mac"
elsif OS.doze?
return "win"
end
我已经创建了一个快速测试项目
您可以通过以下方式运行此操作:
git clone git://github.com/trinitronx/rspec-stubmock-test.git
cd rspec-stubmock-test/
bundle install
rspec
我尝试手动重写
OS.*?
方法,但似乎不起作用。我如何才能做到这一点?您应该可以这样存根方法:
describe "#get_os" do
subject { TestMe.new.get_os }
context "on a linux machine" do
before do
OS.stub(linux?: true, mac?: false, doze?: false, bits: 64)
end
it { should eq 'linux64' }
end
context "on a mac" do
before do
OS.stub(linux?: false, mac?: true, doze?: false, bits: 64)
end
it { should eq 'mac' }
end
# etc etc
end
您应该可以这样存根方法:
describe "#get_os" do
subject { TestMe.new.get_os }
context "on a linux machine" do
before do
OS.stub(linux?: true, mac?: false, doze?: false, bits: 64)
end
it { should eq 'linux64' }
end
context "on a mac" do
before do
OS.stub(linux?: false, mac?: true, doze?: false, bits: 64)
end
it { should eq 'mac' }
end
# etc etc
end