shoulda-matchers RSpec 期望语法 [英] shoulda-matchers RSpec expect syntax

查看:36
本文介绍了shoulda-matchers RSpec 期望语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 shoulda-matchers 和 RSpec 的新 预期语法?

What is the correct format for using shoulda-matchers and RSpec's new expect syntax?

推荐答案

当然可以使用 shoulda-matchers 和新的 expect 语法如下:

While one could certainly use the shoulda-matchers with the new expect syntax as follows:

it 'should validate presence of :email' do
  expect(subject).to validate_presence_of :email
end

或更简洁但可读性较差:

or the more concise but less readable:

it { expect(subject).to validate_presence_of :email }

这些匹配器通常使用的单行 should 格式在 2.14 中得到明确支持,即使 config.syntax == :expect 也是如此.should 与隐式主题一起使用时:

the one-liner should format these matchers are typically used with is explicitly supported in 2.14 even when config.syntax == :expect. When should is being used with an implicit subject as in:

describe User
  it { should validate_presence_of :email }
end

它不依赖应该依赖的Kernel的猴子补丁.

it does not rely on the monkey patching of Kernel that should otherwise depends on.

https://github.com/rspec/rspec-expectations/blob/master/Should.md.事实上,该文档甚至使用上面的 should 匹配器示例来说明此异常.

This is covered in https://github.com/rspec/rspec-expectations/blob/master/Should.md. In fact, that documentation even uses the above shoulda matcher example to illustrate this exception.

另见使用隐式的`subject`和`expect`在 RSpec-2.11 中,它讨论了一个配置选项,可让您用作 it 的替代方案.

See also Using implicit `subject` with `expect` in RSpec-2.11, which discusses a configuration option which lets you use as an alternative to it.

expect_it { to validate_presence_of :email }

更新:从 RSpec 3.0 (beta2) 开始,您还可以使用:

Update: As of RSpec 3.0 (beta2), you will also be able to use:

it { is_expected.to validate_presence_of :email }

这篇关于shoulda-matchers RSpec 期望语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆