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?

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.