gpt4 book ai didi

ruby - 在描述之外使用 RSpec 的 "expect"等...它会阻止

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

我正在构建一个网络应用程序自动化框架,旨在允许:

  • 当然是自动化任务
  • 轻松构建测试场景

我正在使用 Capybara 与浏览器通信,我有一个组件库,其中包含许多辅助函数(login_to_the_back_office、create_customer 等)。

现在我希望我的组件可以独立使用,也可以在 RSpec 测试中使用。这意味着我的组件(包含在库中)没有环绕 describe... it...默认情况下会阻塞,但在测试使用它们时它们会在某个时候出现,因此它们应该使用 expect和 friend 尽可能多。

我关注了rspec's .should fails (outside describe/it block) in Ruby 2?启用 should并且它有效(实际上只需要 rspec-expectations 在我的情况下就足够了),但我不知道如何让 expect 工作。

我认为expect在这里定义 https://github.com/rspec/rspec-expectations/blob/master/lib/rspec/expectations/syntax.rb但我对 Ruby 元魔法的了解太有限,无法弄清楚如何让我的类(class)可以使用 expect。

我试过包括 RSpec::Expectations::Syntax在我的课上,但我仍然得到undefined method 'expect'每当我尝试使用它时。

如何使用 expectdescribe... it...之外?

最佳答案

包含 ::RSpec::Matchers

class A
include ::RSpec::Matchers
def test
expect('1'.to_i).to eq 1
end

def failed_test
expect('1'.to_i).to eq 2
end
end
A.new.test
# => true
A.new.failed_test
# RSpec::Expectations::ExpectationNotMetError:
# expected: 2
# got: 1

关于ruby - 在描述之外使用 RSpec 的 "expect"等...它会阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23624655/

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