Senjai
Senjai

Reputation: 1816

Understanding assert_equal in Test::Unit with Ruby on Rails

I have the following code:

test "unique title" do
    product = Product.new(title:       products(:ruby).title,
                          description: 'yyy',
                          price:       1,
                          image_url:   "fred.gif")

    assert !product.save
    puts product.errors[:title].join('; ')
    assert_equal "has already been taken", product.errors[:title].join('; ')
  end

The test passes. But I fail to understand why the assert_equal doesn't trigger an error.

Because "has already been taken" is not equal to: ProductTest#test_unique_titlehas already been taken

Which is the output of the puts statement.

Why does this test pass?

Upvotes: 0

Views: 2563

Answers (2)

usha
usha

Reputation: 29349

Completely out of your question. But you don't have to save the record to check if its valid. You can call valid? method to have the errors populated.

test "unique title" do
    product = Product.new(title:       products(:ruby).title,
                          description: 'yyy',
                          price:       1,
                          image_url:   "fred.gif")

    assert !product.valid?
    assert_equal "has already been taken", product.errors[:title].join('; ')
end

Upvotes: 1

oldergod
oldergod

Reputation: 15010

I think you are getting confused because there is somehow no new line in your output. ProductTest#test_unique_title describes the test method you are running and has already been taken is the result of your puts so the test should pass

Upvotes: 2

Related Questions