测试 Minitest assert 和 must 返回的结果不一样,bug?

toctan · 2013年09月12日 · 最后由 toctan 回复于 2013年09月12日 · 2821 次阅读
require 'delegate'

class Bar < SimpleDelegator
  def initialize(obj)
    super(obj)
  end
end

class Foo
end

require 'minitest/autorun'

describe Bar do
  subject { Bar.new(Foo.new) }

  it "assert kind of Bar" do
    assert_kind_of(Bar, subject)
  end

  it "must be kind of Bar" do
    subject.must_be_kind_of Bar
  end

  it "assert instance of Bar" do
    assert_instance_of(Bar, subject)
  end

  it "must be instance of Bar" do
    subject.must_be_instance_of Bar
  end
end
➜  ~  /usr/bin/ruby mini.rb
Run options: --seed 24089

# Running:

F.F.

Finished in 0.004433s, 902.2708 runs/s, 902.2708 assertions/s.

  1) Failure:
Bar#test_0004_must be instance of Bar [mini.rb:30]:
Expected #<Foo:0x00000002f49320> to be an instance of Bar, not Foo.


  2) Failure:
Bar#test_0002_must be kind of Bar [mini.rb:22]:
Expected #<Foo:0x00000002f3ee20> to be a kind of Bar, not Foo.

4 runs, 4 assertions, 2 failures, 0 errors, 0 skips

因为 must_be_kind_of 也被代理了,等效于

Foo.new.must_be_kind_of Bar

可以自己手动覆盖下

subject { Bar.new(Foo.new) }
before { subject.extend MiniTest::Expectations }

#2 楼 @doitian 哦,懂了,谢谢啊。 那 Avdi Grimm 在 http://objectsonrails.com/#sec-12_1 这里的测试写错了?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号