Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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 => ''

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top