Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting mocked model with RSpec2 returning 0 items and no response body
    primarykey
    data
    text
    <p>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.</p> <p>I have defined a simple RSpec as follows to test a FormsController with essentially no logic.</p> <pre><code>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 =&gt; :json response.should be_success end it "finds all forms" do Form.should_receive(:all).and_return([@form]) get :index, :format =&gt; :json Rails.logger.info "*** response.body="+response.body end end </code></pre> <p>Form controller code is very simple currently.</p> <pre><code>class FormsController &lt; 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 =&gt; @forms } end end end </code></pre> <p>When I run the spec, "finds all forms" always fails with </p> <pre><code> Failure/Error: Form.should_receive(:all).and_return([@form]) (&lt;Form(id: integer, title: string, owner_id: integer, created_at: datetime, updated_at: datetime) (class)&gt;).all(any args) expected: 1 time received: 0 times </code></pre> <p>The output from log/test.log shows:</p> <pre><code>*** response.body=[] </code></pre> <p>Why? I feel that the problem stems from <code>Form.stub!(:all).and_return([ @form ])</code>, but I am not sure how to debug.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload