我无法测试抛出的AASM错误。
这是我的控制器方法:
# controllers/jobs_controller.rb
def change_state
respond_to do |format|
if @job.close && @job.save
format.html { redirect_to @job, notice: 'Job has been closed.' }
format.json { render :show, status: :ok, location: @job }
else
format.html { render :show, notice: 'Job could not be closed.' }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
我的规格如下:
# spec/controllers/jobs_controller_spec.rb
describe "POST #change_state" do
it "cannot transition job from closed" do
job.aasm_state = "closed"
job.save!
post :change_state, params: {id: job.hash_id, user_id: user.id}
expect { response }.to raise_error(AASM::InvalidTransition)
end
end
测试失败(预期/期望的失败):
Failure/Error: if @job.close && @job.save
AASM::InvalidTransition:Event 'close' cannot transition from 'closed'.
我只是无法找出通过测试的正确语法。我尝试了expect
行的一些变体,但似乎无法将它拼凑在一起。
感谢任何指导。
答案 0 :(得分:0)
异常发生在expect
语句之前。试试吧:
expect {
post(:change_state, params: { id: job.hash_id, user_id: user.id })
}.to(
raise_error(AASM::InvalidTransition)
)