mardi 16 avril 2019

Trying to understand how Rails testing works and having trouble with a model

Using https://guides.rubyonrails.org/testing.html to learn how to develop automated tests. I ran rails db:test:prepare then edited the default ./test/model/user_test.rb (I have a users model). This test appears very simple but I get the following error:

Mysql2::Error: Field 'screen_name' doesn't have a default value

This should be true as I requested the field not be Null and I don't want a default value. I want a value entered. The database schema is set up this way.

No matter what I do I get this error. I tried running the default controller test and its gets the same error.

I was hopping to deduce what this test is really doing as there is no real description. Maybe this is not a valid test just some scaffolding?

A test to get a failure for not entering a value is certainly a test I need eventually. It seems I'm not getting the instruction I need from this guide, is there a better place to start.

I tried the following:

@User = users.new @User.screen_name = "Name" ... @User.save

got the same result, it seems that just running a test against this model tries to create a null table entry before I do anything. Is that true? If so how do I deal with it and not provide any defaults.

The default controller test gets the exact same result.

./test/model/user_test.rb:

require 'test_helper'

class UserTest < ActiveSupport::TestCase test "the truth" do assert true end end

MySQL Table Desc:

mysql> desc users; +-----------------+--------------+------+-----+---------+-------------- | Field | Type | Null | Key | Default | Extra | +-----------------+--------------+------+-----+---------+-------------- | id | bigint(20) | NO | PRI | NULL | auto_increment | | screen_name | varchar(255) | NO | | NULL | | | email | varchar(255) | NO | | NULL | | | password_digest | varchar(255) | NO | | NULL | | | created_at | datetime | NO | | NULL | | | updated_at | datetime | NO | | NULL | | +-----------------+--------------+------+-----+---------+--------------

I did not know what to expect but I did not expect what I got.

Over the years I have learned start with the default and build up.

Running via Spring preloader in process 15156 Run options: --seed 50091

Running:

E

Error: UserTest#test_the_truth: Mysql2::Error: Field 'screen_name' doesn't have a default value

bin/rails test test/models/user_test.rb:4

Finished in 0.006803s, 146.9987 runs/s, 0.0000 assertions/s. 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

Aucun commentaire:

Enregistrer un commentaire