我在使用ruby hash键格式和 let
时感到困惑。
这在正常情况下起作用。
{
"id" => 1,
"description" => "test 3",
"difficulty" => { "id" => 1, "description" => "easy" },
}
但在let
块中失败
这是代码:
describe 'incorrect_question' do
let(:wrong_question1) {
"id" => 1,
"description" => "test 3",
"difficulty" => { "id" => 1, "description" => "easy" },
}
it 'does something' do
# ...
end
end
它导致以下例外:
syntax error, unexpected =>, expecting '}' (SyntaxError)
"id" => 1,
^
- 如果您的块跨越一行以上,则使用
do
/end
。 -
上面完成后,您会发现您缺少开放的
{
和关闭哈希的}
:let(:wrong_question1) do { "id" => 1, "description" => "test 3", "difficulty" => { "id" => 1, "description" => "easy" } } end