3

I want to unit test an application using shoulda.

In the test i'm doing

User.create!(name: "James")

When i run the test i'm getting the following error:

ActiveRecord::StatementInvalid: Mysql2::Error: Field 'name' doesn't have a default value: INSERT INTO `users` (`created_at`, `updated_at`, `id`) VALUES ('2014-04-07 12:03:07', '2014-04-07 12:03:07', 980190962)

Has this something to do with rails 4 strong parameters?

How can i solve this?

secador de pelo
  • 687
  • 5
  • 26

3 Answers3

3

Check if you have invalid fixtures laying around. Try deleting/fixing test/fixtures/users.yml

Note: You should get full stacktraces in the unit tests by disabling the backtrace silencers in config/initializers/backtrace_silencers.rb.

reto
  • 16,189
  • 7
  • 53
  • 67
  • This was my problem because of the generated .yml fixtures that were created earlier by rails. Make sure that all of your other fixtures are commented out or fixed to be correct. Thanks! – DustinA Dec 07 '14 at 05:06
0

Has this something to do with rails 4 strong parameters?

No, Strong Parameters have nothing to do with this because you are creating your object directly from a hash and not from any ActionController::Parameters.

The issue comes from somewhere else (probably your database).

As I can see from this article, a solution to your problem could be to migrate your field like that:

change_column :users, :name, :string, :limit => 255, :null => false, :default => ''
Dimitri
  • 2,023
  • 18
  • 23
0

I got similar issue while running unit tests. This may happen if your mysql DB connection is in strict mode. Disable strict mode by adding setting in database.yml as below and try.

test:
  database:
    host: localhost
    name: test_db
    username: username
    password: password    
    strict: false
vitthal-gaikwad
  • 1,184
  • 11
  • 13