我希望让用户只能删除他们自己创建的作业(而不是其他人的作业)。我已经验证我的应用程序确实通过“rails server”执行此操作。然而,测试结果很奇怪。
这里是有问题的测试:
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin" do
[...]
describe "authorization" do
[...]
describe "correct user control" do
let(:user) { FactoryGirl.create(:user) }
let(:job) { FactoryGirl.create(:job, user: user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
let(:wrong_job) { FactoryGirl.create(:job, user: wrong_user) }
before { sign_in user }
[...]
describe "users can only delete their own jobs" do
it "should not change job count" do
expect do
delete job_path(wrong_job)
end.to_not change(Job, :count)
end
end
describe "users can delete their own jobs" do
it "should decrease job count" do
expect do
delete job_path(job)
end.to change(Job, :count).by(-1)
end
end
end
end
end
end
除了,我得到了这个奇怪的输出:
失败:
1) Authentication signin authorization correct user control users can only delete their own jobs should not decrease job count
Failure/Error: expect do
count should not have changed, but did change from 0 to 1
# ./spec/requests/authentication_pages_spec.rb:68:in `block (6 levels) in <top (required)>'
2) Authentication signin authorization correct user control users can delete their own jobs should decrease job count
Failure/Error: expect do
count should have been changed by -1, but was changed by 1
# ./spec/requests/authentication_pages_spec.rb:75:in `block (6 levels) in <top (required)>'
为什么工作数量会增加?为什么测试没有按预期进行?
其他信息:
jobs_controller.rb
class JobsController < ApplicationController
skip_before_action :require_signin, only: [:index, :show]
skip_before_action :correct_user, only: [:index, :show, :new, :create]
before_action :set_job, only: [:show, :edit, :update, :destroy]
[...]
def destroy
@job.destroy
respond_to do |format|
format.html { redirect_to jobs_url }
format.json { head :no_content }
end
end
private
def set_job
@job = Job.find(params[:id])
end
def job_params
params.require(:job).permit(:title, :org, :internship, :postdate, :filldate, :location, :link, :description)
end
end
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_filter :require_signin
before_filter :correct_user
include SessionsHelper
private
def require_signin
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
def correct_user
@job = current_user.jobs.find_by(id: params[:id])
redirect_to root_url if @job.nil?
end
end
rake 路
Prefix Verb URI Pattern Controller#Action
jobs GET /jobs(.:format) jobs#index
POST /jobs(.:format) jobs#create
new_job GET /jobs/new(.:format) jobs#new
edit_job GET /jobs/:id/edit(.:format) jobs#edit
job GET /jobs/:id(.:format) jobs#show
PATCH /jobs/:id(.:format) jobs#update
PUT /jobs/:id(.:format) jobs#update
DELETE /jobs/:id(.:format) jobs#destroy
[...]
最佳答案
有两个问题:
正如 Jacek 所提到的,
let
是惰性求值的。这意味着在使用变量之前, block 内的代码不会运行。由于job
变量是在expect
block 中首次使用,因此job
计数增加。如果您希望立即运行let
block 中的代码,请使用let!
方法。get
、put
、patch
和delete
应该在 Controller 测试中使用,而不是集成测试:
# jobs_controller_spec.rb delete :destroy, id: job
In integration tests, you should use Capybara to simulate user input:
# users_messing_around_with_jobs_spec.rb
click_button("Delete")
在您的测试中,delete
没有达到您的预期:出于某种原因,它被映射到 jobs#index
。
关于ruby-on-rails - RSpec 的错误行为期望使用 destroy 更改计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21872463/