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 6591939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:29:25+00:00 2026-05-25T17:29:25+00:00

Sorry for the vague question title, but I’m having a hard time trying to

  • 0

Sorry for the vague question title, but I’m having a hard time trying to distil what I’m asking into a one-liner…

I have a couple of simple Rails (3.1) models generated via rails g scaffold, which I’m trying to unit test. They are defined in the following way:

class ModelA < ActiveRecord::Base
  validates_presence_of :field1, :field2
  validates_uniqueness_of :field1
end

class ModelB < ActiveRecord::Base
  validates_presence_of :field1
  validates_uniqueness_of :field1
end

I have a couple of fixtures for each model, i.e:

model_a_no_field1:
    field2: test

model_a_no_field2:
    field1: test

model_a_ok:
    field1: test
    field2: test

and

model_b_no_field1:

model_b_ok:
    field1: test

My unit tests are testing these validations:

class ModelATest < ActiveSupport::TestCase
  test "field1 should be present" do
    assert !model_as(:model_a_no_field1).valid?
  end

  test "field2 should be present" do
    assert !model_as(:model_a_no_field2).valid?
  end

  test "field1 should be unique" do
    model_a = model_as(:model_a_ok)
    model_a.save

    assert !model_as(:model_a_ok).valid?
  end
end

These tests all pass correctly. However, my similar unit tests for ModelB:

class ModelBTest < ActiveSupport::TestCase
  test "field1 should be present" do 
    assert !model_bs(:model_b_no_field1).valid?
  end

  test "field1 should be unique" do 
    model_b = model_bs(:model_b_ok)
    model_b.save

    assert !model_bs(:model_b_ok).valid?
  end
end

Fail on the second test (testing the uniqueness).

I’m almost certain this is to do with the empty YAML fixture for model_b_no_field1, but I’m not sure. I can cause the test to pass by replacing the test method body with

test "field1 should be unique" do
  model_b = model_bs(:model_b_ok)
  model_b.save

  model_b2 = ModelB.new

  assert !model_b2.valid?
end

Which is fine, but I’d like to understand what’s happening here.

  • 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-25T17:29:26+00:00Added an answer on May 25, 2026 at 5:29 pm

    I think you’re subverting the test infrastructure that rails creates for you. The models that you have in the yaml file should be valid, and if you want to test something that is invalid, create it in the test.

    Creating the data in the test has the extra value of being very clear and concrete. Otherwise a dev would need to open the yaml file to understand what data you have and what is missing.

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

Sidebar

Related Questions

Sorry about a vague title, but here's what i am trying to do. I
Sorry about the vague title, but i didnt know how to ask the question
Sorry for vague title, I am not able to describe my question in one
Sorry about the vague question title, but I have these typedefs here: typedef std::list<AnimationKeyframe>
Sorry for the vague thread title; hard to succinctly describe my question. I have
I'm sorry the question title is so vague, but I was looking at some
Sorry for a vague title but it's hard to be more specific. Platform used:
Sorry for vague question title. I've got a table containing huge list of, say,
Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
Sorry if this question is too vague, but I'd rather not muddy it's point

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.