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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:10:42+00:00 2026-05-11T20:10:42+00:00

Hey all, I am completely lost on this one. I found a code snippet

  • 0

Hey all, I am completely lost on this one.

I found a code snippet online to help validate fields via ajax as the user types into them. So I’m trying to write a spec against part of it and I just can’t get it to pass.

Here’s the code

def validate
  field = params[:field]
  user = User.new(field => params[:value])
  output = ""
  user.valid?
  if user.errors[field] != nil
    if user.errors[field].class == String
      output = "#{field.titleize} #{user.errors[field]}" 
    else
      output = "#{field.titleize} #{user.errors[field].to_sentence}" 
    end
  end
  render :text => output
end

and here is my test so far

  describe "POST validate" do
    it "retrieves the user based on the past in username" do
      mock_errors ||= mock("errors")
      mock_errors.stub!(:[]).and_return(nil)
      User.should_receive(:new).with({'username'=>"UserName"}).and_return(mock_user)
      mock_user.should_receive(:valid?).and_return(true)
      mock_errors.should_receive(:[]).with("username").and_return(nil)
      put :validate, :field=>'username', :value=>'UserName'      
      response.should == ""
    end
  end

I get this error –

1) Spec::Mocks::MockExpectationError
in ‘UsersController POST validate
retrieves the user based on the past
in username’ Mock ‘errors’ received
unexpected message :[] with
(“username”)

I can’t seem to figure out how in the world to mock the call to user.errors[field]. Ideally this spec tests the happy path, no errors. I’ll then write another for a validation failure.

  • 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-11T20:10:42+00:00Added an answer on May 11, 2026 at 8:10 pm

    I’m not seeing mock_user. Here’s a shot at it:

    describe "POST validate" do
      it "retrieves the user based on the past in username" do
        mock_errors = mock("errors")
        mock_user = mock("user")
        mock_user.stub!(:errors).and_return([mock_errors])
        mock_errors.stub!(:[]).and_return(nil)
        User.should_receive(:new).with({'username'=>"UserName"}).and_return(mock_user)
        mock_user.should_receive(:valid?).and_return(true)
        mock_errors.should_receive(:[]).with("username").and_return(ActiveRecord::Errors.new({}))
        put :validate, :field=>'username', :value=>'UserName'      
        response.should == ""
      end
    end
    

    The key is that you need your User mock to respond to the errors method by returning either an empty hash or a hash of fieldname/errors. An alternative to this is to use one of the fixture replacement tools. I’m using machinist right now, which might reduce this whole thing to:

    describe "POST validate" do
      it "retrieves the user based on the past in username" do
        @user = User.make{'username'=>"UserName"}
        @user.should_receive(:valid?).and_return(true)
        @user.errors.should_receive(:[]).with("username").and_return(ActiveRecord::Errors.new({}))
        put :validate, :field=>'username', :value=>'UserName'      
        response.should == ""
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, I'm pulling my hair out on this one. I've checked all my
Hey, in a tutorial C++ code, I found this particular piece of confusion: PlasmaTutorial1::PlasmaTutorial1(QObject
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey all, I am not quite getting why this doesn't seem to be working.
hey all.i'm newbie at this problem.i have this data in table result: item range_code
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey all, I have something of an interesting requirement for my project. I need
Hey all. We're sending quite a few emails (around 23k) using IIS6 SMTP service
Hey all - I have an app where I'm authenticating the user. They pass
Hey all. I have a server written in java using the ServerSocket and Socket

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.