gpt4 book ai didi

ruby - rspec : it { is_expected. 中的这个符号是什么...}

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

我正在阅读 'Better specs'页面,并在其中一个示例中说:

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

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

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

这是什么语法?

最佳答案

这是在 Rspec 3.XX 中大量引入的内容。它位于概述的单行语法指南下 here

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.

This comes in two flavors:

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

关于ruby - rspec : it { is_expected. 中的这个符号是什么...},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25729154/

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