我正在寻求学习RSpec。目前我正在学习内置匹配器。
我在expect(actual).to be_kind_of(expected)上有点困惑
在relishapp站点上,它说be_kind_of的行为是
obj.should be_kind_of( type ):调用obj.kind_of?(type),如果类型在obj的类层次结构中或是模块,并且包含在obj类层次结构中的类中,则返回true。
APIdock状态这个例子
module M; end
class A
include M
end
class B < A; end
class C < B; end
b.kind_of? A #=> true
b.kind_of? B #=> true
b.kind_of? C #=> false
b.kind_of? M #=> true但是,当我在RSpec上测试它时,当我这样做时,它会返回false:
module M; end
class A
include M
end
class B < A; end
class C < B; end
describe "RSpec expectation" do
context "comparisons" do
let(:b) {B.new}
it "test types/classes/response" do
expect(b).to be kind_of?(A)
expect(b).to_not be_instance_of(A)
end
end
end
1) RSpec expectation comparisons test types/classes/response
Failure/Error: expect(b).to be kind_of?(A)
expected false
got #<B:70361555406320> => #<B:0x007ffca7081be0>为什么我的RSpec返回false,而这个例子说它应该返回true
发布于 2016-09-18 08:10:18
你在写
expect(b).to be kind_of?(A)但最重要的是
expect(b).to be_kind_of(A)注意下划线和没有问号。您所写的测试将通过
b.equal?(kind_of?(A))您是在Rspec测试本身上调用#kind_of?,而不是像使用matcher那样调用b。
发布于 2016-09-18 06:58:19
你把两种匹配混为一谈,expect。查看rspec-期望的文档
expect(actual).to be_an_instance_of(expected) # passes if actual.class == expected
expect(actual).to be_a(expected) # passes if actual.kind_of?(expected)
expect(actual).to be_an(expected) # an alias for be_a
expect(actual).to be_a_kind_of(expected) # another alias你应该选择两者兼用,或者其中之一。
https://stackoverflow.com/questions/39554664
复制相似问题