Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4582646
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:06:16+00:00 2026-05-21T21:06:16+00:00

This seems a little weird, when running a unit test to check if the

  • 0

This seems a little weird, when running a unit test to check if the length of my title is > 10 my test will pass if I include “assert product.invalid?” before any my other assert like this:

require 'test_helper'

class ProductTest < ActiveSupport::TestCase

test "product title is too short" do
    product = Product.new(:title => "My lady",
                          :description => "yyy",
                          :price       => 1
                         )
    assert product.invalid?
    assert_equal "must be atleast 10 characters long.", product.errors[:title].join('; ')
end
end

However if I don’t include “assert product.invalid?” before assert_equal I get this error
1) Failure: test_product_title_is_too_short blah blah blah
(“must be atleast 10 characters long.”) expected but was (“”).

Is that how Test::Unit works? I have to assert that something is valid or invalid before I proceed with other tests? Kind of like initializing the test?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:06:17+00:00Added an answer on May 21, 2026 at 9:06 pm

    This isn’t actually a characteristic of your test framework, but rather of ActiveRecord.

    When creating an object with ActiveRecord, you can assign validations to ensure certain things about the attributes of the objects (as you have on your object). However, these validations only get run at certain times, and as you’re observing, the ‘new’ method isn’t one of these times. However, by asking about the validity of the object with the invalid? method, you have thus triggered the validations.

    I think what might be more natural is to use the “create” method instead of ‘new’ to trigger validations for your object. create checks validations automatically which will eliminate your call to “invalid?” in your test and should still populate the errors hash as desired:

    product = Product.create(:title => "My lady",
                          :description => "yyy",
                          :price       => 1
                         )
    assert_equal "must be atleast 10 characters long.", product.errors[:title].join('; ')
    

    Similarly to the ‘create’ method is the ‘create!’ method which will actually raise an exception if any validations fail. create will simply return false and populate the error hash.

    For more info on validations check out:
    http://guides.rubyonrails.org/active_record_validations_callbacks.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm not sure if it's just me, but this seems a little weird. I
I only read a little bit about this topic, but it seems that the
Seems I've outdone myself. All the while I was creating this pretty little 'latest
The title may seem a little weird but what I find around the internet
Ok, this is a little weird. Ignore what I am trying to do, and
Ok this is a little weird and it doesn't seem to should work this
So I'm getting experience on this stuff little by little, but this problem seems
I apologise if this seems a little basic, but I'm new to WCF -
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
This seems to be an overlooked area that could really use some insight. What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.