Awesome. &nbsp;I totally get it, but is that how you&#39;re always supposed to spec out associations and all the methods that go with an association like &quot;create&quot; and such? &nbsp;I&#39;m interested in that because I&#39;m specing a lot of code that deals heavily with code that has associations going on, yet none of the examples of RSpec use I come across have anything about associations and RSpec. &nbsp;I don&#39;t imagine they&#39;d be handled too differently, but it&#39;s just something wondered about. &nbsp;<br>
<br>Right now, I&#39;m testing and building up a controller and so far my tests have been passing as long as I don&#39;t run into any examples that involve associations. &nbsp;This code, for example, has been giving me a few issues:<br>
<br># my users_controller_spec.rb (or at least, the relevant part)<br><br><div>describe UsersController do</div><div>&nbsp;&nbsp;fixtures :users</div><div>&nbsp;&nbsp;</div><div>&nbsp;&nbsp;before(:each) do</div><div>&nbsp;&nbsp; &nbsp;@tiffani = users(:tiffani)</div>
<div>&nbsp;&nbsp; &nbsp;@users = [@tiffani]</div><div>&nbsp;&nbsp; &nbsp;@users.stub!(:build).and_return(@tiffani)</div><div>&nbsp;&nbsp; &nbsp;@mock_account = mock_model(Account, :users =&gt; @users, :save =&gt; true)</div><div>&nbsp;&nbsp;end</div><div>&nbsp;&nbsp;</div><div>&nbsp;&nbsp;describe &quot;when creating a User&quot; do</div>
<div>&nbsp;&nbsp; &nbsp;it &quot;should return the registration form for adding a User on GET new&quot; do</div><div>&nbsp;&nbsp; &nbsp; &nbsp;User.should_receive(:new).once</div><div>&nbsp;&nbsp; &nbsp; &nbsp;get :new</div><div>&nbsp;&nbsp; &nbsp;end</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;it &quot;should render the new User registration form on GET new&quot; do</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp;get :new</div><div>&nbsp;&nbsp; &nbsp; &nbsp;response.should render_template(&quot;users/new&quot;)</div><div>&nbsp;&nbsp; &nbsp;end</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;it &quot;should create a new User and then redirect to that User&#39;s profile on POST create&quot; do</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp;@mock_account.should_receive(:new)</div><div>&nbsp;&nbsp; &nbsp; &nbsp;post :create, :new_user =&gt; { :first_name =&gt; @tiffani.first_name, :last_name =&gt; @tiffani.last_name,</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :email_address =&gt; @tiffani.email_address&nbsp;&nbsp;}</div>
<div>&nbsp;&nbsp; &nbsp;end</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;it &quot;should redirect to users/new when User is setup with invalid data&quot;</div><div>&nbsp;&nbsp;end<br><br>&nbsp;&nbsp;# ...<br>end<br><br>And in users_controller.rb:<br><br><div>def create</div>
<div>&nbsp;&nbsp; &nbsp;@new_account = Account.new(params[:account])</div><div>&nbsp;&nbsp; &nbsp;@new_user = @new_account.users.build(params[:new_user])</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;respond_to do |wants|</div><div>&nbsp;&nbsp; &nbsp; &nbsp;if @new_account.save</div><div>
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;flash[:notice] = &quot;Welcome, #{ @new_user.first_name }!&quot;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;wants.html { redirect_to(user_url(@new_user)) }</div><div>&nbsp;&nbsp; &nbsp; &nbsp;else</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;wants.html { render :action =&gt; &quot;new&quot; }</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp;end</div><div>&nbsp;&nbsp; &nbsp;end</div><div>&nbsp;&nbsp;end<br></div></div><br><br>When I run the tests the third test fails and RSpec complains that &quot;<span class="Apple-style-span" style="color: rgb(194, 0, 0); font-family: -webkit-monospace; font-size: 11px; white-space: pre; ">Mock &#39;Account_1003&#39; expected :new with (any args) once, but received it 0 times&quot;<br>
<br></span>I&#39;m confused about that since I am calling Account.new in the create method on the controller. &nbsp;What&#39;s really wrong here?<br><br>Thanks in advance for answering my RSpec questions! :D<br><br>--Tiffani AB<br>
<br><br><div class="gmail_quote">On Thu, Jul 3, 2008 at 9:09 PM, Mikel Lindsaar &lt;<a href="mailto:raasdnil@gmail.com">raasdnil@gmail.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
On Fri, Jul 4, 2008 at 8:32 AM, Tiffani Ashley Bell<br>
&lt;<a href="mailto:tiffani2k3@gmail.com">tiffani2k3@gmail.com</a>&gt; wrote:<br>
&gt; Hi everybody,<br>
<br>
Hi Tiffany, welcome to Rspec<br>
<div class="Ih2E3d"><br>
&gt; I was reading the Typo source code, however, and came across some code that<br>
&gt; I didn&#39;t know exactly how it worked. &nbsp;I&#39;ve noticed that in testing one of<br>
&gt; their controllers, they use a variable (@comments) that they don&#39;t declare<br>
&gt; anywhere else, yet they use it as a stand in for collections on some of the<br>
&gt; mocks. &nbsp;How is that possible? &nbsp;I know in the mocking documentation it says<br>
&gt; that you can define collaborations with other objects before those objects<br>
&gt; exist, but how is that working in this code? &nbsp;I only ask that because later,<br>
&gt; you see code like this: &nbsp;@comments.stub!(:build).and_return(@comment).<br>
<br>
</div>If you have a look at the descriptions, they use :shared =&gt; true.<br>
This is a way of being DRY in RSpec (which I personally don&#39;t think is<br>
such a good idea).<br>
<br>
What the shared =&gt; true declaration allows you to do is to include<br>
that block of code elsewhere with &#39;it should behave like my shared<br>
code&#39;<br>
<br>
So we have (describe &quot;All Requests&quot;, :shared =&gt; true do)<br>
<br>
and then the next description block is:<br>
<br>
describe &quot;General Comment Creation&quot;, :shared =&gt; true do<br>
 &nbsp;it_should_behave_like &quot;All Requests&quot;<br>
<br>
Which then includes the All Requests block (which is just a before method).<br>
<br>
The @comments variable gets declared in:<br>
<div class="Ih2E3d"><br>
@comments.stub!(:build).and_return(@comment)<br>
<br>
</div>and then this is tied in to the Article model in the _previous_ code<br>
block like so:<br>
<br>
 &nbsp; &nbsp;@article &nbsp;= mock_model(Article,<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:comments &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =&gt; @comments,<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:published_comments &nbsp; &nbsp; &nbsp; &nbsp; =&gt; @comments,<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:add_comment &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=&gt; @comment)<br>
<br>
<br>
So when you call @article.comments you get @comments as a stub back<br>
which stubs :build and returns a @comment.<br>
<br>
Ugh.<br>
<br>
This is where, in RSpec, you can dig a very fast grave. &nbsp;Because<br>
you&#39;ll come back to this code in 6-12 months and be totally stuck<br>
trying to figure out what is where.<br>
<br>
I recently wrote a viewpoint on this that might help you:<br>
<a href="http://www.lindsaar.net/2008/6/24/tip-24-being-clever-in-specs-is-for-dummies" target="_blank">http://www.lindsaar.net/2008/6/24/tip-24-being-clever-in-specs-is-for-dummies</a><br>
<br>
Hope you do well with Rspec, feel free to ask more questions!<br>
<font color="#888888"><br>
--<br>
<a href="http://lindsaar.net/" target="_blank">http://lindsaar.net/</a><br>
Rails, RSpec, Puppet and Life blog....<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" target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>
</font></blockquote></div><br>