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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:22:15+00:00 2026-05-17T18:22:15+00:00

I am trying to create a rspec test for a model for States. This

  • 0

I am trying to create a rspec test for a model for States. This state model has an association to a Country model. My factories look like

Factory.define :country do |country|
  country.name  "Test Country"
end

Factory.define :state do |state|
  state.name "Test State"
  state.association :country
end

I have made a functional state model rspec but I am not sure if the way I set up the state @attr is correct or a hack

require 'spec_helper'

describe State do
  before(:each) do
    country = Factory(:country)
    @attr = { :name => 'Test State', :country_id => country.id }
  end

  it "should create a new state given valid attributes" do
    State.create!(@attr)
  end  
end

Being new to rails/rspec I’m unsure if forcibly saying :country_id => country.id is correct or a cheap way of solving the issue. I appreciate any help or suggestions I can get.

I am also including both models just in case.

class Country < ActiveRecord::Base
  has_many :states
  attr_accessible :name

  validates :name,  :presence => true,
                    :uniqueness => {:case_sensitive => false}
end

class State < ActiveRecord::Base
  belongs_to :country

  attr_accessible :name, :country_id

  validates :name,  :presence => true,
                    :uniqueness => {:case_sensitive => false, :scope => :country_id}

  validates :country_id, :presence => true
end
  • 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-17T18:22:16+00:00Added an answer on May 17, 2026 at 6:22 pm

    This is a good start, but your test isn’t actually testing anything. As a general rule, you should always have one “should” call per “it” block.

    Here’s another way of looking at it, assuming the same factories and models:

    require 'spec_helper'
    
    describe State do
      before(:each) do
        @state = Factory.build(:state)
      end
    
      it "should create a new state given valid attributes" do
        @state.save.should be_true
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to expect an error in an rspec test. lambda {Participant.create!({:user_id => three.id,
I'm trying to use factory_girl to create a user factory (with RSpec) however it
Trying to create a user account in a test. But getting a Object reference
I'm trying to perform an integration test via Watir and RSpec . So, I
I am trying to capture screenshots on test failure using selenium-client and rspec. I
Trying to create a grails ant task that has other environments besides prod for
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to create my first iPhone app that would play back audio. When I
Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different

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.