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

Ask A Question

Stats

  • Questions 200k
  • Answers 200k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As stated on this page: Thread.Sleep is unique amongst the… May 12, 2026 at 7:59 pm
  • Editorial Team
    Editorial Team added an answer I try to avoid putting anything significant in the Program.cs… May 12, 2026 at 7:59 pm
  • Editorial Team
    Editorial Team added an answer Installing a C++ library means specifying to interested software (eg.… May 12, 2026 at 7:59 pm

Related Questions

I am developing an auto-builder that will run a series of steps in our
Hey all, I am trying to test if the argument passed into my function
Hey all. I am working on a project for school where we are given
Hey all, I am wondering what the best way to do the following is:

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.