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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:59:08+00:00 2026-05-27T20:59:08+00:00

I am attempting to create an API with Rails using BDD with RSpec. Rails

  • 0

I am attempting to create an API with Rails using BDD with RSpec.
Rails version is 3.1.1, Ruby version is 1.9.2, Devise version is 1.5.3, and rspec version is 2.7.0. I am relatively new to Rails and very new to RSpec.

I have defined a simple RSpec as follows to test a FormsController with essentially no logic.

describe FormsController, " handling GET /forms" do
  include Devise::TestHelpers
  render_views

  before do        
    user = Factory.create(:user) # Handle Devise authentication
    user.confirm!
    sign_in user

    @form = mock_model(Form)
    Form.stub!(:all).and_return([ @form ])
  end

  it "gets successfully" do
    get :index, :format => :json
    response.should be_success
  end

  it "finds all forms" do
    Form.should_receive(:all).and_return([@form])
    get :index, :format => :json
    Rails.logger.info "*** response.body="+response.body
  end
end

Form controller code is very simple currently.

class FormsController < ApplicationController
  before_filter :authenticate_user!

  # GET /forms
  # GET /forms.json
  def index
    @forms = Form.find_all_by_owner_id(current_user.id)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @forms }
    end
  end
end

When I run the spec, “finds all forms” always fails with

 Failure/Error: Form.should_receive(:all).and_return([@form])
   (<Form(id: integer, title: string, owner_id: integer, created_at: datetime, updated_at: datetime) (class)>).all(any args)
       expected: 1 time
       received: 0 times

The output from log/test.log shows:

*** response.body=[]

Why? I feel that the problem stems from Form.stub!(:all).and_return([ @form ]), but I am not sure how to debug.

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-27T20:59:08+00:00Added an answer on May 27, 2026 at 8:59 pm

    After much more trial and error, the following solution worked for me.

    • I migrated from mocking the Form model to using Factory Girl to create the full model
    • I then updated the test to use to_json to compare the response against the model.

    The spec is as follows.

    describe FormsController, " handling GET /forms" do
      include Devise::TestHelpers
      render_views
    
      before do        
        user = Factory.create(:user) # Handle Devise authentication
        user.confirm!
        sign_in user
    
        @form1 = Factory.create(:form)
      end
    
      it "gets successfully" do
        get :index, :format => :json
        response.should be_success
      end
    
      it "finds all forms" do
        get :index, :format => :json
        response.body.should == [ @form1 ].to_json
        Rails.logger.info "*** response.body="+response.body
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an API wrapper for Issuu using ruby and am running
I have the following code: using (MemoryStream str = new MemoryStream()) { Program.api.GetDocument(result, str);
I am attempting to load products using the Magento Core API product.create method. Magento
I am attempting to create a very basic login screen, using the Storyboard designer
I am attempting to create a REST API in PHP and I'd like to
I am attempting to create new membership users in an Ektron CMS400.NET-based website by
I'm attempting to Create a Job and Add a Batch using the Salesforce Bulk
In attempting to implement fileUpload using the streaming API I get an error described
I'm attempting to create a very large image in Java like so: BufferedImage bi
I'm using the .NET SDK and attempting to tag a new instance immediately after

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.