0

现在我断言调用了一个方法:

代码:

def MyClass
  def send_report
    ...
    Net::SFTP.start(@host, @username, :password => @password) do |sftp|
      ...
    end
    ...
  end
end

测试:

it 'successfully sends file' do
  Net::SFTP.
    should_receive(:start).
    with('bla.com', 'some_username', :password => 'some_password')

  my_class.send_report
end

但是,我还想在调用 Net::SFTP.start 时检查给定条件是否为真。我该怎么做这样的事情?

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
4

3 回答 3

1

您可以向 提供一个块should_receive,该块将在调用该方法时执行:

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
于 2012-11-29T07:34:06.007 回答
0

你可以使用期望

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
于 2012-11-29T07:28:45.220 回答
0

谢谢@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
于 2012-11-29T07:51:55.177 回答