views:

20

answers:

0

I'm trying to test creation of a new model inside my unit tests, and I'm getting some puzzling behavior.

In this example, I already have a fixture that define an Article :valid_article:

public_review = Review.new({:article => articles(:valid_article)})
assert !public_review.article_id.nil?

Oddly enough, this fails the assertion, because the article isn't being properly set. However, the following works:

public_review = Review.new({:article => articles(:valid_article)})    
public_review.article = articles(:valid_article)
assert !public_review.article_id.nil?

I don't know why I need to assign the article attribute directly, as it should have already been assigned, but it doesn't take unless I do so apparently. I also tried the following, and this fails as well:

public_review = Review.new
public_review.attributes = {:article => articles(:valid_article)}
assert !public_review.article_id.nil?

Note that this is a simplified scenario, and there are other attributes I would like to be assigning at the same time, which is why I'd prefer not to have to assign attributes individually. Other directly assigned attributes, such as public => true are mass-assigned properly, and pass subsequent assertions. I have also tried mass-assignments to article_id as opposed to article, but this also doesn't work.

Is there some rule or characteristic of fake models built by fixtures for testing that makes you unable to use them for mass assignment?