Unit test error: field ... doesn't have a default value

Hello!

here is my unit test:

require ‘test_helper’

class UsuarioTest < ActiveSupport::TestCase

Replace this with your real tests.

#test “the truth” do
#assert true
#end

def test_salvar
usuario = Usuario.new
assert usuario.save
end

def test_dois
assert true
end
end

  1. Error:
    test_dois(UsuarioTest):
    ActiveRecord::StatementInvalid: Mysql::Error: Field ‘usuario_id’ doesn’t
    have a
    default value: INSERT INTO albums (…

  2. Error:
    test_salvar(UsuarioTest):
    ActiveRecord::StatementInvalid: Mysql::Error: Field ‘usuario_id’ doesn’t
    have a
    default value: INSERT INTO albums (…

2 tests, 0 assertions, 0 failures, 2 errors

The relationship is: Usuario has_many :albums

  1. Error:
    test_dois(UsuarioTest):
    ActiveRecord::StatementInvalid: Mysql::Error: Field ‘usuario_id’ doesn’t
    have a
    default value: INSERT INTO albums (…

This is saying ‘I tried to insert the fixtures you defined but mysql
didn’t like the data you tried to insert’

Fred

Thanks Fred!

I didn’t understand why it inserts an album because an Usuario can or
can not have an Album. :confused:

Frederick C. wrote:

� 1) Error:
test_dois(UsuarioTest):
ActiveRecord::StatementInvalid: Mysql::Error: Field ‘usuario_id’ doesn’t
have a
default value: INSERT INTO albums (…

This is saying ‘I tried to insert the fixtures you defined but mysql
didn’t like the data you tried to insert’

Fred

2009/6/15 Le Sa [email protected]:

Thanks Fred!

I didn’t understand why it inserts an album because an Usuario can or
can not have an Album. :confused:

I am not sure from your reply whether you still have a problem, if you
do then the problem may be a syntax error in one of your fixture
files.

Colin

On Jun 15, 7:03 pm, Le Sa [email protected] wrote:

albums.yml
funk:
nome: funk
foto: funk.jpg
usuario: leonardo

There is no Usuario “leonardo”, but the Album “funk” is saved anyway.

your validations aren’t run when inserting fixtures. in addition the
foxy fixtures stuff that means that you can write leonardo to mean ’
the users fixtures with label usario’ does no validation of the label:
it just generates the id that it knows would be used if there was one

Fred

Thanks Colin and sorry that I was not clear. I already fixed the
fixtures.

But one thing is strange.
usuarios.yml
leandro:
nome: Leandro
email: [email protected]
data_nascimento: 2009-05-25

albums.yml
funk:
nome: funk
foto: funk.jpg
usuario: leonardo

There is no Usuario “leonardo”, but the Album “funk” is saved anyway.
usuario_test.rb
… def test_new() assert Usuario.new; end …

Colin L. wrote:

2009/6/15 Le Sa [email protected]:
I am not sure from your reply whether you still have a problem, if you
do then the problem may be a syntax error in one of your fixture
files.

Colin