将 RSpec 匹配器拆分为多行
Break RSpec matcher to multiple lines
有没有办法将长 RSpec 行分成两行:
expect(....).to
eq(.....)
?
更新:
现在我有一个错误:
Failure/Error:
expect(@query_builder.questions_from_time(@time_to_test)).to
ArgumentError:
The expect syntax does not support operator matchers, so you must pass a matcher to #to
.
如果我删除换行符,错误就会消失
to
在技术上只是一种方法,但常见的风格是在 rspec 中的 to
方法上去掉括号。但是,Ruby 解析器似乎没有意识到您正在尝试向该 to
方法发送一个参数,如果您将它分隔到一个没有括号的新行。
以下任何一项都应该有效……
expect(....).
to eq(.....)
或
expect(....)
.to eq(.....)
或
expect(....).to eq(
.....
)
或
expect(
....
).to eq(.....)
或
expect(
....
).to eq(
.....
)
我猜它的长短就是"don't break before an argument that isn't surrounded by parenthesis"。至于使用其中的哪一个——取决于特定的代码。我会做任何最容易阅读的事情,并保持行的长度相当短。
您可以在行尾使用反斜杠向 ruby 表明该行继续:
expect(...).to \
eq(...)
有没有办法将长 RSpec 行分成两行:
expect(....).to
eq(.....)
?
更新:
现在我有一个错误:
Failure/Error: expect(@query_builder.questions_from_time(@time_to_test)).to ArgumentError: The expect syntax does not support operator matchers, so you must pass a matcher to
#to
.
如果我删除换行符,错误就会消失
to
在技术上只是一种方法,但常见的风格是在 rspec 中的 to
方法上去掉括号。但是,Ruby 解析器似乎没有意识到您正在尝试向该 to
方法发送一个参数,如果您将它分隔到一个没有括号的新行。
以下任何一项都应该有效……
expect(....).
to eq(.....)
或
expect(....)
.to eq(.....)
或
expect(....).to eq(
.....
)
或
expect(
....
).to eq(.....)
或
expect(
....
).to eq(
.....
)
我猜它的长短就是"don't break before an argument that isn't surrounded by parenthesis"。至于使用其中的哪一个——取决于特定的代码。我会做任何最容易阅读的事情,并保持行的长度相当短。
您可以在行尾使用反斜杠向 ruby 表明该行继续:
expect(...).to \
eq(...)