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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:17:06+00:00 2026-05-31T20:17:06+00:00

I have been developing a sample application after reading Rails 3 Tutorial book. In

  • 0

I have been developing a sample application after reading Rails 3 Tutorial book. In this application there are many Sections and each section has one Video.

Here are the artifacts of this application:

Models

class Video < ActiveRecord::Base
  has_many :sections
end

class Section < ActiveRecord::Base
  belongs_to :video

  validates :name, presence: true, length: { maximum: 50 }
end

RSpec

require 'spec_helper'

describe Section do

    let(:video) { Video.new(name: "Dummy Video Name", path: "/home/data/video/test.mov") }

  before do
    @section = video.sections.build(name: 'Test Section')
  end

    subject { @section }

    # Check for attribute accessor methods
    it { should respond_to(:name) }

    it { should respond_to(:video) }
    it { should respond_to(:video_id) }

    its(:video) { should == video }

    # Sanity check, verifying that the @section object is initially valid
    it { should be_valid }

    describe "when name is not present" do
        before { @section.name = "" }
        it { should_not be_valid }
    end

    describe "when name is too long" do
      before { @section.name = "a" * 52 }
      it { should_not be_valid }
    end

    describe "when video_id not present" do
        before { @section.video_id = nil }
            it { should_not be_valid }
    end
...
end

And the schema definitions of both Models

..
create_table "sections", :force => true do |t|
    t.string   "name"

    t.integer  "video_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

create_table "videos", :force => true do |t|
    t.string   "name"
    t.string   "path"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end
...

While running above rspec I am getting following error.

Failures:

  1) Section 
     Failure/Error: it { should be_valid }
       expected valid? to return true, got false
     # ./spec/models/section_spec.rb:22:in `block (2 levels) in <top (required)>'

  2) Section video 
     Failure/Error: its(:video) { should == video }
       expected: #<Video id: nil, name: "Dummy Video Name", path: "/home/data/video/test.mov", created_at: nil, updated_at: nil>
            got: nil (using ==)
     # ./spec/models/section_spec.rb:19:in `block (2 levels) in <top (required)>'

I could map my requirement with User-Micropost relation describe in the book and aligned RSpec with them. I have limited knowledge on Rails and the whole echo system.

Please help me to resolve this issue and some reference to validation Models with RSpec(and shoulda) is highly appreciable.

  • Amit Patel
  • 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-31T20:17:07+00:00Added an answer on May 31, 2026 at 8:17 pm

    I am able to resolve this issue by saving video in before block.

    Here is the snippet

    before do
      video.save
      @section = video.sections.build(name: 'Test Section')
    end
    

    The only difference between the Micropost model spec of Rails 3 Tutorial book and the above one is , the former one is using FactoryGirl#create. From https://github.com/thoughtbot/factory_girl/wiki/How-factory_girl-interacts-with-ActiveRecord I found that FactoryGirl.create method actually creates and save instance internally. So it was working there while it was no working in my code.

    If you have better insight with RSpec for ActiveRecord then please share with us.

    Thanks.

    Amit Patel

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

Sidebar

Related Questions

I have been developing a C# windows form application in XP. It all works
I have been developing a project and in this project i have designed my
I have been developing a web application for almost a year which I had
I have been developing Android application, and I have one question - ListView uses
I'm developing an iPad application. I have been provided with a PNG image that
I have been developing an iphone application using a domain model, and have put
I have been developing an application with Apache Felix as my OSGi runtime for
I am developing a stock/e-commerce web application. We have been using a version of
i have been working on developing an blackberry application. as per now i want
I am a new to developing for Android and have been reading up and

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.