[rspec-users] I'm really bad at controllers, help please.

Fischer, Daniel daniel at danielfischer.com
Sat Jun 2 04:27:32 EDT 2007


Hey,

Sorry for so many questions - I'm really bad at this right now.

I'm trying to cover the following code w/ rspec

  def index
    if params[:user_id]
      @user = User.find(params[:user_id])
      @messages = @user.messages
    end
  end

So basically what I'm doing is listing all the messages for a user,
provided there is an id parameter.

describe MessagesController, " handling GET /messages for a user" do

  before do
    @message = mock_model(Message)
    @message.stub!(:user_id).and_return(1)
    @user = mock_model(User)
    @user.stub!(:id).and_return(1)
    User.stub!(:messages).and_return([@message])
    User.stub!(:find).and_return([@user])
  end

  def do_get
    get :index, :user_id => 1
  end

  it "should be successful" do
    do_get
    response.should be_success
  end

  it "should render index template" do
    do_get
    response.should render_template('index')
  end

  it "should find all messages" do
    User.should_receive(:messages).and_return([@message])
    do_get
  end

  it "should assign the found messages for the view" do
    do_get
    assigns[:messages].should == [@message]
  end
end


I'm trying to use the basic scaffold spec, but I'm absolutely clueless
on what the proper way to handle this is, I'm not even sure the proper
way I should mock the messages method, so it'll return a "stub?" of a
collection, or whatever the proper term is.

I'm sorry you have to deal with a newb, but if you could really help
me out I'd appreciate it a ton, thanks!
-- 
-Daniel Fischer


More information about the rspec-users mailing list