[rspec-users] Could anyone please help with rspec/nested resource behavior checking?

Anthony Carlos anthony at digitalphenom.com
Sun Jun 3 23:44:54 EDT 2007


Daniel,

Which of these examples is failing? Or, are you asking a more general  
question about how to use a mock or a stub? I'm going through my own  
pains trying to learn this stuff, so my perspective might be close to  
yours! Please scroll down; I have a couple of in-line questions for ya!

On Jun 2, 2007, at 4:31 AM, Fischer, Daniel wrote:

> Jonathon,
>
> Not sure - I don't really know what to do at this point, but I have a
> new problem again lol.
>
> Anyway, I can't seem to create new mailing messages? I guess I ask  
> to many...
>
> Well, forgive me - but I'll just attach it here since I can't make  
> a new one.
>
> 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.
>

Can you tell me where you found the basic scaffold spec? I haven't  
seen it yet. I've been learning by studying the docs from the rspec  
site.

Are you having a problem ensuring that the @messages variable in your  
controller has an array of messages after a GET?

I think I can explain the use of a stub or mock if this is really  
your question.

Thanks,

-Anthony





More information about the rspec-users mailing list