gpt4 book ai didi

ruby-on-rails - RSpec kind_of?返回错误结果

转载 作者:行者123 更新时间:2023-12-02 04:05:58 25 4
gpt4 key购买 nike

我正在努力学习 RSpec。目前正在研究built-in matchers .

我对 expect(actual).to be_kind_of(expected) 有点困惑

关于 relishapp site , 它表示 be_kind_of 的行为是

obj.should be_kind_of(type): calls obj.kind_of?(type), which returns true if type is in obj's class hierarchy or is a module and is included in a class in obj's class hierarchy.

APIdock 状态 this example :

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>

当示例说明它应该返回 true 时,为什么我的 RSpec 返回 false?

最佳答案

你在写

expect(b).to be kind_of?(A)

但是匹配器是

expect(b).to be_kind_of(A)

注意下划线和缺少问号。如果

b.equal?(kind_of?(A))

您在 Rspec 测试本身上调用 #kind_of?,而不是像使用匹配器那样调用 b

关于ruby-on-rails - RSpec kind_of?返回错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39554664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com