I'm in Chapter 10 of the Foundation Rails 2 book. We're working with RSpec.
We're testing the 'index' action of the 'PluginsController'.
Here's the code for the controller:
class PluginsController < ApplicationController
# GET /plugins
# GET /plugins.xml
def index
@plugins = Plugin.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @plugins }
end
end
Here's the code for the tests for that controller:
require File.dirname(__FILE__) + '/../spec_helper'
describe PluginsController, " GET to /plugins" do
before do
@plugin = mock_model(Plugin)
Plugin.stub!(:find).and_return([@plugin])
end
it "should be successful" do
get :index
response.should be_success
end
it "should find a list of all plugins" do
Plugin.should_receive(:find).with(:all).and_return([@plugin])
get :index
end
it "should assign the list of plugins to a variable to be used in the view"
it "should render the index template"
end
When we were writing our test, I thought that this line
Plugin.should_receive(:find).with(:all).and_return([@plugin])
should have had
@plugins
and not
@plugin
because in the controller we have
def index
@plugins = Plugin.find(:all)
I wanted to see what would happen if I changed
Plugin.should_receive(:find).with(:all).and_return([@plugin])
to
Plugin.should_receive(:find).with(:all).and_return([@plugins])
and the test passed.
So...why is it @plugin and not @plugins? And...why does the test pass with both?
Cheers!