Jonathan,<br><br>Thanks a lot - that accomplished what I wanted to do, and provided me with insight.<br><br>I'm still having some sort of misunderstanding though, because I'm having an error like this now (in a different section though; this time regarding post:
<br><br>2)<br>Spec::Mocks::MockExpectationError in 'MessagesController handling POST /messages should create a new message'<br>Mock 'Message_1026' received unexpected message :author_id= with (202018)<br>/Users/sparta/Projects/work/idastudios/podff_machine/config/../app/controllers/messages_controller.rb:31:in `create'
<br>/Users/sparta/Projects/work/idastudios/podff_machine/config/../vendor/plugins/haml/lib/sass/plugin.rb:116:in `process_without_test'<br>./spec/controllers/messages_controller_spec.rb:206:in `do_post'<br>./spec/controllers/messages_controller_spec.rb:211:
<br><br>yet I've tried both Message.stub!(:author_id).and_return(1) and @message.stub!(:author_id).and_return(1) why would it still fail after stubbing both possibilities?<br><br>In my controller i'm calling @message.author_id
= current_user.id<br><br>Thanks!<br><br><div><span class="gmail_quote">On 6/2/07, <b class="gmail_sendername">Jonathan Linowes</b> <<a href="mailto:jonathan@parkerhill.com">jonathan@parkerhill.com</a>> wrote:</span>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">fyi, i'm finding the should_receive(:messages) to be too brittle<br>because any time I add a call like @
user.messages.find or whatever it<br>breaks my spec (requiring I do .twice or .times(N) . IMO the spec<br>shouldn't be so sensitive to implementation of the behavior, so i'm<br>now stubbing the association<br><br>
replaced<br> @user.should_receive(:messages).and_return(@messages)<br>with<br> @user.stub!(:messages).and_return(@messages)<br><br><br><br>On Jun 2, 2007, at 3:09 PM, Jonathan Linowes wrote:<br><br>> I'm nearly as new at this as you are I'm I'm just figuring things out
<br>> myself too.<br>><br>> I think your problem is method :messages is being called on an<br>> instance of User, not the class, so you need<br>><br>> @user.stub!(:messages).and_return([@message])<br>>
<br>> here's some clues to how I've gotten similar things working<br>> To diagnose problems with my examples, I<br>> - watch the test.log file. You could use 'tail' or on mac use the<br>> 'console' utility
<br>> - use the -e option in spec to run one example at a time. I copy/<br>> paste the test name from the terminal window to avoid a lot of typing<br>> - sometimes I comment out code in my app methods to limit what its
<br>> actually doing and then add one line at a time to "re-develop" the<br>> method<br>> - similarly I may create an extra test method in my controller or<br>> model to isolate things I dont understand between the spec and the
<br>> code.<br>><br>> I also tend to use should_receive rather than stubs, it provides more<br>> details (although seems to make the tests more brittle)<br>><br>> thus,<br>><br>> # separate for reuse with multiple describe cases
<br>> def setup_messages do<br>> @user = mock_model(User)<br>> @message = mock_model(Message)<br>> @messages = [@message]<br>> end<br>><br>> def before(:each) do<br>> setup_messages<br>
> User.should_receive(:find).with("1").and_return(@user)<br>> @user.should_receive(:messages).and_return(@messages)<br>> end<br>><br>> def do_get<br>> get :index, :user_id => 1<br>> end
<br>><br>> it "should find all messages" do<br>> do_get<br>> assigns[:messages].should == @messages<br>> end<br>><br>> I think this is right. Please let me know<br>> :)<br>><br>>
<br>> On Jun 2, 2007, at 4:27 AM, Fischer, Daniel wrote:<br>><br>>> Hey,<br>>><br>>> Sorry for so many questions - I'm really bad at this right now.<br>>><br>>> I'm trying to cover the following code w/ rspec
<br>>><br>>> def index<br>>> if params[:user_id]<br>>> @user = User.find(params[:user_id])<br>>> @messages = @user.messages<br>>> end<br>>> end<br>>>
<br>>> So basically what I'm doing is listing all the messages for a user,<br>>> provided there is an id parameter.<br>>><br>>> describe MessagesController, " handling GET /messages for a user" do
<br>>><br>>> before do<br>>> @message = mock_model(Message)<br>>> @message.stub!(:user_id).and_return(1)<br>>> @user = mock_model(User)<br>>> @user.stub!(:id).and_return(1)
<br>>> User.stub!(:messages).and_return([@message])<br>>> User.stub!(:find).and_return([@user])<br>>> end<br>>><br>>> def do_get<br>>> get :index, :user_id => 1<br>>> end
<br>>><br>>> it "should be successful" do<br>>> do_get<br>>> response.should be_success<br>>> end<br>>><br>>> it "should render index template" do
<br>>> do_get<br>>> response.should render_template('index')<br>>> end<br>>><br>>> it "should find all messages" do<br>>> User.should_receive(:messages).and_return([@message])
<br>>> do_get<br>>> end<br>>><br>>> it "should assign the found messages for the view" do<br>>> do_get<br>>> assigns[:messages].should == [@message]<br>>> end
<br>>> end<br>>><br>>><br>>> I'm trying to use the basic scaffold spec, but I'm absolutely<br>>> clueless<br>>> on what the proper way to handle this is, I'm not even sure the
<br>>> proper<br>>> way I should mock the messages method, so it'll return a "stub?" of a<br>>> collection, or whatever the proper term is.<br>>><br>>> I'm sorry you have to deal with a newb, but if you could really help
<br>>> me out I'd appreciate it a ton, thanks!<br>>> --<br>>> -Daniel Fischer<br>>> _______________________________________________<br>>> rspec-users mailing list<br>>> <a href="mailto:rspec-users@rubyforge.org">
rspec-users@rubyforge.org</a><br>>> <a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>><br>> _______________________________________________<br>
> rspec-users mailing list<br>> <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>> <a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users
</a><br><br>_______________________________________________<br>rspec-users mailing list<br><a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/rspec-users">
http://rubyforge.org/mailman/listinfo/rspec-users</a><br></blockquote></div><br><br clear="all"><br>-- <br>-Daniel Fischer<br><br><a href="http://danielfischer.com">http://danielfischer.com</a> - Geek Blog<br><a href="http://abigfisch.com">
http://abigfisch.com</a> - Portfolio<br><a href="http://writersbeat.com">http://writersbeat.com</a> - Writing Community