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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:12:34+00:00 2026-06-03T23:12:34+00:00

I am finding it very hard to stub certain attributes of a model on

  • 0

I am finding it very hard to stub certain attributes of a model on a controller test. I want to make sure to stub as little as possible.


EDIT: I have been demoved of using stubs for such integration. I understood that the stubs won’t reach the action call. The correct question would now be:

How can one use mocks and stubs to simulate a certain state in a Rails controller test?


So I’ve reached something like the following:

Spec

require 'spec_helper'

describe TeamsController do
  let(:team) { FactoryGirl.create :team }

  context "having questions" do
    let(:competition) { FactoryGirl.create :competition }

    it "allows a team to enter a competition" do
      post(:enter_competition, id: team.id, competition_id: competition.id)

      assigns(:enroll).team.should == team
      assigns(:enroll).competition.should == competition
    end
  end

  # ...
end

Factories

FactoryGirl.define do
  factory :team do
    name "Ruby team"
  end

  factory :competition, class: Competition do
    name "Competition with questions"

    after_create do |competition|
      competition.
        stub(:questions).
        and_return([ 
          "something"
        ])
    end
  end

  factory :empty_competition, class: Competition do
    name "Competition without questions"
    questions []

    after_create do |competition|
      competition.stub(:questions).and_return []
    end
  end
end

Production code

class TeamsController < ApplicationController
  def enter_competition
    @team = Team.find params[:id]
    @competition = Competition.find params[:competition_id]
    @enroll = @team.enter_competition @competition

    render :nothing => true
  end
end

class Team < ActiveRecord::Base
  def enter_competition competition
    raise Competition::Closed if competition.questions.empty?

    enroll = Enroll.new team: self, competition: competition
    enroll.save
    enroll
  end
end

When I run the test, the questions attribute comes as being nil and so the test fails in the model when checking for nil.empty?.

Why isn’t the stub being used so that the state of that message is correctly used? I expected that @competition.questions would be [ "question" ] but instead I get nil.

  • 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-06-03T23:12:36+00:00Added an answer on June 3, 2026 at 11:12 pm

    The problem you’re running into is that stub works on an instance of a Ruby object; it doesn’t affect all ActiveRecord objects that represent the same row.

    The quickest way to fix your test would be to add this to your test, before the post:

    Competition.stub(:find).and_return(competition)
    

    The reason that’s necessary is that Competition.find will return a fresh Competition object that doesn’t have questions stubbed out, even though it represents the same database row. Stubbing find as well means that it will return the same instance of Competition, which means the controller will see the stubbed questions.

    I’d advise against having that stub in your factory, though, because it won’t be obvious what’s stubbed as a developer using the factory, and because it means you’ll never be able to test the real questions method, which you’ll want to do in the Competition unit test as well as any integration tests.

    Long story short: if you stub out a method on an instance of your model, you also need to stub out find for that model (or whatever class method you’re using to find it), but it’s not a good idea to have such stubs in a factory definition.

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

Sidebar

Related Questions

I am newbie and finding it very hard to grasp the syntax of Class
I really like the magento structure but finding things is very hard ;) My
I've been having a very hard time finding good examples of UIScrollView. Even Apple's
I am using Rail 3 and I am finding it very hard to do
I am having a very hard time finding resources that talk about the windows
I am having a very hard time finding a standard pattern / best practice
I am finding it very hard to understand the way the inverse of the
I am having a very hard time finding any documentation online that clearly explains
I have a SeekBar inside a HorizontalScrollView and am finding it very hard to
I am finding it very hard to find examples on most of the queryover

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.