0

在一个精简的项目中演示问题

耙式测试产生:

1) Failure:
test_can_mass_assign_accessible_flds(BrandTest) [ test/unit/brand_test.rb:11]:

Exception raised:
<#<ActiveModel::MassAssignmentSecurity::Error:  \
    Can't mass-assign protected attributes: brand_name, support_num>>.

2 tests, 2 assertions, 1 failures, 0 errors, 0 skips

这两个模型和测试具有并行结构,但在测试时会产生不同的结果。经验告诉我这应该是我的错误,但我看不出在哪里。任何人都可以帮忙吗?

楷模:

class Brand < ActiveRecord::Base
  attr_accessible :brand_name, :support_num, :comment
end

class Mask < ActiveRecord::Base
  attr_accessible :brand_id, :brand_sku, :comment
end

测试:

require 'test_helper'
class BrandTest < ActiveSupport::TestCase
  def setup
    @accessible_flds = { :brand_name => 'MyString', 
                         :support_num => 'MyString', 
                         :comment => 'MyText' }
  end

  test "can mass assign accessible flds" do
    assert_nothing_raised { Mask.new(@accessible_flds) }
  end
end

class MaskTest < ActiveSupport::TestCase
  def setup
    @accessible_flds = { :brand_id => 1, 
                         :brand_sku => 'MyString', 
                         :comment => 'MyText' }
  end

  test "can mass assign accessible flds" do
    assert_nothing_raised { Mask.new(@accessible_flds) }
  end
end

架构:

ActiveRecord::Schema.define(:version => 20121212024505) do

  create_table "brands", :force => true do |t|
    t.string "brand_name"
    t.string "support_num"
    t.text   "comment"
  end

  create_table "masks", :force => true do |t|
    t.integer "brand_id"
    t.string  "brand_sku"
    t.text    "comment"
  end

end

环境:

Windows 7 Pro, SP 1
ruby -v  =>> ruby 1.9.3p327 (2012-11-10) [i386-mingw32]
rails -v =>> Rails 3.2.9
4

1 回答 1

0

您的BrandTest课程仍在引用Mask

Mask.new(@accessible_flds) 

应该

Brand.new(@accessible_flds)
于 2012-12-16T17:28:49.240 回答