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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:47:50+00:00 2026-05-28T14:47:50+00:00

RSpec newbie here. I’m trying to test my models, which have methods that uses

  • 0

RSpec newbie here.

I’m trying to test my models, which have methods that uses update_attributes to update values of other models.

I’m sure that the values are persisted in the database, but they are not passing the specs.

However, when I include something like @user.reload it works.

I’m wondering if I’m doing things wrongly. Specifically, how should one test models which change attributes of other models?

UPDATED with code:

describe Practice do

before(:each) do
    @user = build(:user)
    @user.stub!(:after_create)
    @user.save!

    company = create(:acme)
    course = build(:acme_company, :company => company)
    course.save!

  @practice = create(:practice, :user_id => @user.id, :topic => "Factors and Multiples" )
end

describe "#marking_completed" do

    it "should calculate the points once the hard practice is done" do
        @practice.question_id = Question.find(:first, conditions: { diff: "2" }).id.to_s
        @practice.responses << Response.create!(:question_id => @practice.question_id, :marked_results => [true,true,true,true])
        @practice.save!

        lambda {
            @practice.marking_completed
            @practice.reload # <-- Must add this, otherwise the code doesn't work
        }.should change { @practice.user.points }.by(20)
    end
end

end

In Practice.rb

def marking_completed       

# Update user points
user.add_points(100)
self.completed = true
self.save!

end

In User.rb

def add_points(points)
self.points += points
update_attributes(:points => self.points)

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-28T14:47:51+00:00Added an answer on May 28, 2026 at 2:47 pm

    What’s going on is that the user object is cached on the @practice variable, thus requiring a reload when user has been updated. There isn’t much you can do about this with the current spec, but you might want to think about what you’re actually testing. It seems weird to me that your spec is for the Practice model, yet the assertion should change { @practice.user.points }.by(20) is really describing the User behavior.

    I would personally decouple the the Practice and User models a little more in your spec, and test their behavior independently.

    describe "#marking_completed" do
      before do
        @practice.question_id = Question.find(:first, conditions: { diff: "2" }).id.to_s
        @practice.responses.create!(:question_id => @practice.question_id, :marked_results => [true,true,true,true])
        @practice.save!
      end
    
      it "should calculate the points once the hard practice is done" do
        @practice.user.should_receive(:add_points).with(20).once
        @practice.marking_completed
      end
    end
    

    Then I would add a separate test for the User model:

    describe User do
      it 'should add specified points to a user' do
        lambda { subject.add_points(100) }.should change { subject.points }.by(100)
      end
    end
    

    One other sort of side note is that it’s not clear what Question.find is returning, or why the user’s points changes by 20 in your test.

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

Sidebar

Related Questions

Using RSpec, I want to test that certain methods are called within a method.
Using RSpec and Cancan, I have this test which fails with: Failure/Error: User.should_receive(:new).and_return(@user) expected:
I have written some Rspec test cases in my spec/models/season_spec.rb file. They are as:-
I have a RSpec test for a class in /lib/classes which needs access to
I have the following RSpec example which is passing: describe UsersController do it should
I have problems to get rspec running properly to test validates_inclusion_of my migration looks
I have just installed Rspec and Rspec-rails. When i try to run the test,
I've got an rspec test which looks something like this: require 'require_all' require_rel '../spec_helper'
I am writing rspec test for my Cars class, and have a question regarding
I have an rspec scenario that looks like: scenario some description of the scenario,

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.