2012-03-27 86 views
0

我正在使用Rails 3.2.2。我有一個叫做用戶的設計模型。受保護屬性的單元測試Rails

在我的用戶模式,我設置:

attr_protected :is_admin 

(is_admin是一個布爾屬性)

爲了測試這個屬性我做:

test "should not be able to change to admin" do 
    user = User.create(:name => "Joaquim", :email => "[email protected]", :password => "123456", :is_admin => true) 

    assert user.errors.get(:is_admin), "Cant change admin configuration" 
end 

當我運行這個測試引發錯誤:

1) Error: 
test_should_not_be_able_to_change_to_admin(UserTest): 
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: is_admin 
    test/unit/user_test.rb:44:in `test_should_not_be_able_to_change_to_admin' 

什麼斷言我必須用於此測試?

謝謝!

回答

1

您可以使用

assert_raise(ExceptionClass) { .... } 

在rspec的你會做

expect { ... }.to raise_error(ExceptionClass)