本文介绍了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 将为示例提供一个自动的文档字符串从示例中使用的匹配器生成.这是设计的特别是为了帮助避免在文档的情况下重复字符串和示例中使用的匹配器完全相互映射.当过度使用时,它会产生文档输出没有很好地阅读或有助于理解你的对象描述.

这有两种风格:

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..}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:28
查看更多