加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 使用多个方法调用的RSpec`uj_receive`行为

发布时间:2020-12-17 02:45:38 所属栏目:百科 来源:网络整理
导读:考虑以下测试: class A def print(args) puts args endenddescribe A do let(:a) {A.new} it "receives print" do a.should_receive(:print).with("World").and_call_original a.print("Hello") a.print("World") endend RSpec Documentation说: Use shoul
考虑以下测试:

class A
  def print(args)
    puts args
  end
end

describe A do
  let(:a) {A.new}

  it "receives print" do
   a.should_receive(:print).with("World").and_call_original

   a.print("Hello")
   a.print("World")
 end
end

RSpec Documentation说:

Use should_receive() to set an expectation that a receiver should receive a
message before the example is completed.

所以我期待这个测试通过,但事实并非如此.它失败了以下消息:

Failures:

1) A receives print
 Failure/Error: a.print("Hello")
   #<A:0x007feb46283190> received :print with unexpected arguments
     expected: ("World")
          got: ("Hello")

这是预期的行为吗?有没有办法让这个测试通过?

我使用的是ruby 1.9.3p374和rspec 2.13.1

解决方法

这应该工作:

class A
  def print(args)
    puts args
  end
end

describe A do
  let(:a) {A.new}

  it "receives print" do
   a.stub(:print).with(anything())
   a.should_receive(:print).with("World").and_call_original

   a.print("Hello")
   a.print("World")
 end
end

测试失败了,因为你已经设置了一个精确的期望“应该收到:打印’世界’”,但是rspec注意到一个对象正在接收带有’Hello’的打印方法,因此它没有通过测试.在我的解决方案中,我允许使用任何参数调用print方法,但它仍然以“World”作为参数跟踪调用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读