# Unit Tests and Default Values

**URL:** <https://www.ruby-forum.com/t/unit-tests-and-default-values/104342>\
**Category:** Rails\
**Created:** [May 16, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/unit-tests-and-default-values/104342 "2007-05-16T22:51:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [May 16, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/unit-tests-and-default-values/104342/1 "2007-05-16T22:51:23Z")

</div>

We have a fairly simple model that appears to be throwing an error  
when using unit tests, but we don’t know why. Seems to work well  
enough from our controllers just throws the error in test.  
We have a table that contains a field called user\_id that holds the ID  
of the user who created the post. The definition is:

t.column “user\_id”, :integer, :null =\> false

The post model belongs to :user and user has\_many :posts

This works fine except for unit tests, for some bizzare reason. When  
we perform a simple unit test like

def test\_blank\_post

```
post = Post.create(:user => users(:spanogebob))
assert !post.valid?

```

end

which should assert to true since the body of the post will be blank,  
which is not allowed. However, what we get is a MySQL error:

Field ‘user\_id’ doesn’t have a default value: Insert into  
‘posts’ (‘id’) values (2)

Anyone have any idea why this is happening.

Thanks in advance  
Dale

---

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [May 16, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/unit-tests-and-default-values/104342/2 "2007-05-16T22:57:23Z")

</div>

Does

post = Post.create(:user\_id =\> users(:spanogebob).id)

work for you?

Jason

---

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [May 17, 2007, 2:09am UTC](https://www.ruby-forum.com/t/unit-tests-and-default-values/104342/3 "2007-05-17T02:09:39Z")

</div>

No, unfortunately it’s the same if we do that.

Dale

---

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [May 18, 2007, 1:31am UTC](https://www.ruby-forum.com/t/unit-tests-and-default-values/104342/4 "2007-05-18T01:31:52Z")

</div>

Wow, I’ve been on this for days now and can’t seem to work out where  
I’m going wrong.  
It must be something I’m doing wrong

---

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [May 18, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/unit-tests-and-default-values/104342/5 "2007-05-18T15:16:23Z")

</div>

Hmm. I guess you could write some tests proving that the has\_many /  
belongs\_to relationships exist and are working. Otherwise we’ll need to  
see  
code to help you out.

Jason

---

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [May 18, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/unit-tests-and-default-values/104342/6 "2007-05-18T16:24:30Z")

</div>

Pete,

> ```
> post = Post.create(:user => users(:spanogebob))
> 
> ```
> 
> we get is a MySQL error:

What happens where you don’t use fixtures but real objects :

# fixtures … don’t load any fixture at all

# 

spongebox = User.create( …)  
post = Post.create(:user =\> spanogebob)

## Alain R.

[http://blog.ravet.com](http://blog.ravet.com)
