rspec 中的这个符号是什么:它 { is_expected_to..} [英] What is this notation in rspec: it { is_expected_to..}

查看:50
本文介绍了rspec 中的这个符号是什么:它 { is_expected_to..}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 '更好的规格' 页面,其中一个示例中说:>

I am reading the 'Better specs' page, and in one of the examples it says:

context 'when logged in' do
  it { is_expected.to respond_with 200 }
end
context 'when logged out' do
  it { is_expected.to respond_with 401 }
end

我不认识这个.我通常会这样做:

And I don't recognize this. I usually would do:

context 'when logged out' do
  it 'responds with a 401' do
    expect(response).to eq(401)
  end
end

那是什么语法?

推荐答案

这是 Rspec 3.XX 中大量引入的内容.它位于 here

This is something introduced heavily in Rspec 3.XX. It's under the one line syntax guides as outlined here

RSpec 支持单行语法来设置期望主题.RSpec 将为示例提供一个自动的文档字符串从示例中使用的匹配器生成.这是设计的特别是为了帮助避免在文档的情况下重复字符串和示例中使用的匹配器完全相互映射.当过度使用时,它会产生文档输出没有很好地阅读或有助于理解你的对象描述.

RSpec supports a one-liner syntax for setting an expectation on the subject. RSpec will give the examples a doc string that is auto- generated from the matcher used in the example. This is designed specifically to help avoid duplication in situations where the doc string and the matcher used in the example mirror each other exactly. When used excessively, it can produce documentation output that does not read well or contribute to understanding the object you are describing.

这有两种风格:

is_expected 被简单地定义为 expect(subject) 并且专为当您使用 rspec-expectations 及其较新的基于期望的语法.

is_expected is defined simply as expect(subject) and is designed for when you are using rspec-expectations with its newer expect-based syntax.

这篇关于rspec 中的这个符号是什么:它 { is_expected_to..}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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