我正在使用Test :: Unit对我的rails 3应用程序运行功能测试,该应用程序使用Devise和CanCan来帮助管理用户。但是,当我对我的contacts_controller进行POST或PUT时,我在请求中遇到了一个奇怪的问题。当控制器从数据库加载用户时,请求失败,然后停止,然后导致测试失败。
有人会知道为什么会这样吗?
控制台输出
SQL (2.8ms) describe `roles_users`
SQL (2.1ms) describe `camps_users`
SQL (3.4ms) describe `camps_users`
SQL (2.0ms) describe `campers_camps`
SQL (2.8ms) describe `campers_camps`
SQL (2.1ms) describe `roles_users`
Loaded suite test/functional/camp/contacts_controller_test
Started
SQL (0.1ms) BEGIN
SQL (0.7ms) SHOW TABLES
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE (`users`.`id` = 292811013) LIMIT 1
SQL (0.4ms) SELECT COUNT(*) FROM `contacts`
Camp Load (0.3ms) SELECT `camps`.* FROM `camps` WHERE (`camps`.`id` = 665138414) LIMIT 1
Processing by Camp::ContactsController#create as HTML
Parameters: {"contact"=>{"first_name"=>"John", "last_name"=>"Doe", "email_address"=>"test@test.com", "phone_number"=>"1234567890"}, "camp_id"=>"bolo"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE (`users`.`id` = 292811013) LIMIT 1
Completed in 24ms
SQL (0.3ms) SELECT COUNT(*) FROM `contacts`
SQL (0.1ms) ROLLBACK
F
Finished in 0.385608 seconds.
1) Failure:
test_should_create_contact(Camp::ContactsControllerTest) [test/functional/camp/contacts_controller_test.rb:45]:
"Contact.count" didn't change by 1. <5> expected but was <4>.
1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
Test run options: --seed 10895 --name "test_should_create_contact"
联系测试
test "should create contact" do
assert_difference('Contact.count') do
post :create,
:contact => {
:first_name => "John",
:last_name => "Doe",
:email_address => "test@test.com",
:phone_number => "1234567890"
},
:camp_id => camps(:bolo).uri
end
assert_response :created
assert_not_nil assigns(:contact)
get :show,
:id => assigns(:contact).to_param,
:camp_id => camps(:bolo).uri
assert_response :success
end
联系控制器
class Camp::ContactsController < AuthorizedController
def create
@contact = @camp.contacts.build(params[:contact])
respond_to do |format|
if @contact.save
format.html { render :text => "contact created!", :status => :created }
format.xml {render :xml => @contact, :status => :created, :location => @contact}
else
format.html { render :action => "new", :status => :bad_request }
format.xml { render :xml => @contact.errors, :status => :bad_request }
end
end
end
end
授权控制器
class AuthorizedController < ApplicationController
before_filter :authenticate_user!
check_authorization
load_and_authorize_resource
rescue_from CanCan::AccessDenied do |exception|
flash[:error] = exception.message
respond_to do |format|
format.html { redirect_to new_user_session_path, :status => :unauthorized }
format.xml { render :xml => "...", :status => :unauthorized }
end
end
end
CanCan能力
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.role? :admin
can :manage, :all
elsif user.role? :account_admin
can [:show, :update, :destroy, :create], Camp do |camp|
user.is_linked_to_camp?(camp)
end
can :manage, Contact do |contact|
user.is_obj_linked_to_camp?(contact)
end
else
cannot :manage, :all
end
end
答案 0 :(得分:0)
原来是我的代码中存在错误。我的测试未更新为在Contacts模型中考虑新架构。