我有以下通过的 rspec 请求规范示例
it "increases count by 1" do
attributes = attributes_for(:district)
expect { post admin_districts_path, params: { district: attributes} }.to change { District.count }.by(1)
end
expect
行有点忙,所以我想把它拆开。以下导致错误it "increases count by 1" do
attributes = attributes_for(:district)
block = { post admin_districts_path, params: { district: attributes} }
expect(block).to change { District.count }.by(1)
end
有错误
syntax error, unexpected '}', expecting keyword_end
为什么会发生此错误?有没有更简洁的方法来编写这个规范示例?
最佳答案
此外,您可以创建一个 lambda:
block = -> { post admin_districts_path, params: { district: attributes} }
expect(block).to change { District.count }.by(1)
关于ruby-on-rails - 更改模型计数的更具可读性的请求规范,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41640420/