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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:28:15+00:00 2026-05-18T09:28:15+00:00

I have been following the 15 TDD steps to create a Rails application guide

  • 0

I have been following the 15 TDD steps to create a Rails application guide – but have run into an issue I cannot seem to resolve. For the functional test of the WordsController, I have the following code:

class WordsControllerTest < ActionController::TestCase
  
  test "should get learn" do
    get 'learn'
    assert_response :success
  end

  test "learn passes a random word" do    
    some_word = Word.new
    Word.expects(:random).returns(some_word)
    get 'learn'
    assert_equal some_word, assigns('word')
  end
end

In the Word class I have the following code:

class Word < ActiveRecord::Base
  def self.random
    all = Word.find :all
    all[rand(all.size)]
  end
end

When I run the tests, I experience the following error (shortened for brevity):

1) Failure: unexpected invocation: Word(...).random() satisfied expectations:
- expected exactly once, already invoked once: Word(...).random()

I have tried changing changing the order of the tests along with a multitude of other things, but time and time again I continue to receive the same test failure – that Word.random() has already been invoked.

I’m running Rails 3.0 beta 4 and Mocha 0.9.8. I’ve searched long and hard for a solution to my problem, but I can’t seem to find it. I’m new to Ruby/Rails so am rather unfamiliar with the language and the frameworks.

Thanks in advance!

  • 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-18T09:28:16+00:00Added an answer on May 18, 2026 at 9:28 am

    I had the same problem, mocked functionality was not isolated to a test, it seems to be a problem with the load order of Mocha.

    I had some issues getting Mocha to work with Rails3. I found a few stackoverflow posts regarding, but didn’t stumble across the solution until I found a post on agoragames.com

    Basically, in the Gemfile of your project, the require for Mocha should look like:

    gem 'mocha', :require => false
    

    Then in test/test_helper.rb, add a require line for mocha:

    ...
    ...
    require File.expand_path('../../config/environment', __FILE__)
    require 'rails/test_help'
    require 'mocha'
    
    class ActiveSupport::TestCase
    ...
    ...
    

    I think the require line for mocha in the Gemfile means that you need to already have mocha installed as a gem, bundler won’t take care of it for you.

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

Sidebar

Related Questions

i have been following apps snowfall tutorial, but i am getting some issue in
I have been following a guide to create a shopping cart in MVC3. http://mvcmusicstore.codeplex.com/
Hi I am quite new to php but i have been following some tutorials
I'm a newcomer to Rails, and have been following the tutorial on the rails
In my latest ASP.NET MVC 2 application I have been trying to put into
Have been following Rails Tutorial by Michael Hart rails version 3.0 on mac OS
I have been following the ExtDirectSpring form posting guide closely. I defined an api
I have been following the RestKit installation instructions but I now have an error
I have been following this URL to help me create template views using BrowserView.
I am learning rails by following the Rails Tutorial under Ubuntu. I have been

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.