Hello,
I’m trying to write a unit test for model that doesn’t have an ‘id’
field. I created the model’s table with the following migration, making
sure the primary key was set.
class CreateActiveUserMetrics < ActiveRecord::Migration
def self.up
create_table :active_user_metrics,
{:id => false,
:primary_key => :sample_date} do |t|
t.column :sample_date, :date
t.column :less_than_thirty_days, :integer
t.column :less_than_ninety_days, :integer
t.column :all_days, :integer
end
end
def self.down
drop_table :active_user_metrics
end
end
The test I wrote is as follows.
class ActiveUserMetricTest < Test::Unit::TestCase
fixtures :active_user_metrics
def test_invalid_with_empty_attributes
metric = ActiveUserMetric.new
assert metric.invalid?
assert metric.errors.invalid?(:sample_date)
assert metric.errors.invalid?(:less_than_thirty_days)
assert metric.errors.invalid?(:less_than_ninety_days)
assert metric.errors.invalid?(:all_days)
end
end
When I run the test I get the following error, complaining about the
lack of an ‘id’ field.
Loaded suite test/unit/active_user_metric_test
Started
EE
Finished in 0.781 seconds.
-
Error:
test_invalid_with_empty_attributes(ActiveUserMetricTest):
ActiveRecord::StatementInvalid: Mysql::Error: #42S22Unknown column ‘id’
in ‘field list’: INSERT INTO active_user_metrics (id
) VALUES (2)
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract_adapter.rb:120:in
log' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:184:in
execute’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:288:in
insert_fixtures' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:287:in
each’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:287:in
insert_fixtures' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:257:in
create_fixtures’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:257:in
each' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:257:in
create_fixtures’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in
transaction' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:255:in
create_fixtures’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:794:in
silence' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:248:in
create_fixtures’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:565:in
load_fixtures' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:512:in
setup’ -
Error:
test_invalid_with_empty_attributes(ActiveUserMetricTest):
NoMethodError: You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.-
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:112:in
unlock_mutex' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:534:in
teardown’
1 tests, 0 assertions, 0 failures, 2 errors
What should I do to solve this problem? Any help is appreciated.
Thanks,
Justin