gpt4 book ai didi

ruby - 我如何在 PORO 中使用 RSpec Expectations

转载 作者:数据小太阳 更新时间:2023-10-29 08:38:02 25 4
gpt4 key购买 nike

嗨,

我正在尝试擦干我的一些规范。我提取了一个 Assertion 类,它执行几个 should ......但是大多数 RSpec 期望魔术不再起作用。

我将尝试构建一个简单的示例来说明我的问题。

被测对象:

class Foo
def has_bar?; true; end
end

我的断言类:

class MyAssertions
def self.assert_everything_is_ok
@foo = Foo.new
@foo.has_bar?.should == true # works!
@foo.has_bar?.should be_true # undefined local variable or method `be_true`
@foo.should have_bar # undefined local variable or method `have_bar`
end
end

我的规范:

it "tests something" do
@foo = Foo.new
@foo.should have_bar # works!
MyAssertion.assert_everything_is_ok # does not work, because of errors above
end

为什么我不能在我的普通旧 ruby​​ 对象中使用 rspec 期望的语法糖?

最佳答案

经过更多尝试,我想到了这个解决方案:

class MyAssertions
include RSpec::Matchers

def assert_everything_is_ok
@foo = Foo.new
@foo.has_bar?.should == true # works!
@foo.has_bar?.should be_true # works now :)
@foo.should have_bar # works now :)
end
end

诀窍是包含 RSpec::Matchers 模块。而且我使用了实例方法而不是类方法

关于ruby - 我如何在 PORO 中使用 RSpec Expectations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22959785/

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