Ruby RSpec-调用方法时的检查条件?
现在我断言调用了一个方法:Ruby RSpec-调用方法时的检查条件?,ruby,rspec,Ruby,Rspec,现在我断言调用了一个方法: it 'sends a file with the correct arguments' do Net::SFTP.should_receive(:start) do |url, username, options| url.should == 'bla.com' username.should == 'some_username' options[:password].should == 'some_password' <s
it 'sends a file with the correct arguments' do
Net::SFTP.should_receive(:start) do |url, username, options|
url.should == 'bla.com'
username.should == 'some_username'
options[:password].should == 'some_password'
<some condition>.should be_true
end
my_class.send_report
end
代码:
测试:
但是,我还想检查在调用Net::SFTP.start时给定的条件是否为true。我该怎么做这样的事
it 'successfully sends file' do
Net::SFTP.
should_receive(:start).
with('bla.com', 'some_username', :password => 'some_password').
and(<some condition> == true)
my_class.send_report
end
它“成功发送文件”吗
Net::SFTP。
应接收(:开始)。
使用('bla.com','some_username',:password=>'some_password')。
和(=真)
我的班级。发送报告
结束
您可以使用expect
it 'successfully sends file' do
Net::SFTP.
should_receive(:start).
with('bla.com', 'some_username', :password => 'some_password')
my_class.send_report
end
it 'should verify the condition also' do
expect{ Net::SFTP.start(**your params**) }to change(Thing, :status).from(0).to(1)
end
您可以提供一个块,以便在调用该方法时执行
it 'sends a file with the correct arguments' do
Net::SFTP.should_receive(:start) do |url, username, options|
url.should == 'bla.com'
username.should == 'some_username'
options[:password].should == 'some_password'
<some condition>.should be_true
end
my_class.send_report
end
它“发送具有正确参数的文件”do
Net::SFTP.应该接收(:start)do | url、用户名、选项吗|
url.should==“bla.com”
username.should=='some_username'
选项[:密码]。应=='some\u password'
.应该是真的
结束
我的班级。发送报告
结束
谢谢@rickyrickyrice,你的回答几乎是正确的。问题是它没有验证传递给Net::SFTP.start
的参数的正确数量。以下是我最终使用的:
it 'sends a file with the correct arguments' do
Net::SFTP.should_receive(:start).with('bla.com', 'some_username', :password => 'some_password') do
<some condition>.should be_true
end
my_class.send_report
end
它“发送具有正确参数的文件”do
Net::SFTP.should_receive(:start)。使用('bla.com','some_username',:password=>'some_password')可以
.应该是真的
结束
我的班级。发送报告
结束
我看不出这是如何回答这个问题的。您想检查条件吗?您可以在单独的规范中进行检查,因为我在上面给出了示例。您假设条件与SFTP的状态有关。我明白情况未必如此。
it 'sends a file with the correct arguments' do
Net::SFTP.should_receive(:start).with('bla.com', 'some_username', :password => 'some_password') do
<some condition>.should be_true
end
my_class.send_report
end