Thanks on the reminder that &quot;new&quot; is a class method. &nbsp;Plus, I figured using mocks and fixtures together was probably a crappy idea. &nbsp;I&#39;ll be mocking from now on...<br><br>--Tiffani AB<br><br><div class="gmail_quote">
On Sat, Jul 5, 2008 at 5:00 PM, Steve Eley &lt;<a href="mailto:steve@deepsalt.com">steve@deepsalt.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="Ih2E3d">On Sat, Jul 5, 2008 at 3:50 PM, Tiffani Ashley Bell<br>
&lt;<a href="mailto:tiffani2k3@gmail.com">tiffani2k3@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; When I run the tests the third test fails and RSpec complains that &quot;Mock<br>
&gt; &#39;Account_1003&#39; expected :new with (any args) once, but received it 0 times&quot;<br>
&gt;<br>
&gt; I&#39;m confused about that since I am calling Account.new in the create method<br>
&gt; on the controller. &nbsp;What&#39;s really wrong here?<br>
<br>
</div>The problem there is that Account.new is a _class_ method on the<br>
Account class. &nbsp;The @mock_account you made is an _instance_ of Account<br>
(actually, not even that, it&#39;s a mock object that will pretend it&#39;s an<br>
Account if you ask it). &nbsp;You&#39;re not sending @mock_account any<br>
messages, you&#39;re sending them to the Account class. &nbsp;To do what you<br>
want, you need to stub that class, for instance:<br>
<br>
Account.stub!(:new).and_return(@mock_account)<br>
<br>
And in the spec you can do Account.should_receive(:new).<br>
<br>
There&#39;s some other stuff in that spec that looks a bit messy...<br>
Generally speaking, you can do some pretty clean tests with fixtures<br>
*or* you can do tests by mocking everything, but it&#39;s not a great idea<br>
to do both at the same time. &nbsp;In controller specs, best practice is<br>
usually to mock your models and not touch the real models or the<br>
database (i.e. fixtures) at all, because A.) it&#39;s faster and B.)<br>
you&#39;re isolating your tests to *just* the controller code, and won&#39;t<br>
have to worry about tests failing because the models are broken.<br>
(That&#39;s what the model specs are for.) &nbsp;&gt;8-&gt;<br>
<br>
I&#39;m also unclear on the relationship between User and Account in this<br>
code, and why you&#39;re creating a new account for every new user in the<br>
UsersController... &nbsp;But that&#39;s really about your application, not<br>
about RSpec. &nbsp;If that&#39;s how your application needs to behave, then<br>
your spec here seems to be on the right track.<br>
<br>
I hope this was helpful. &nbsp;I&#39;m just figuring a lot of this out myself,<br>
and my main reason for answering you was to reinforce this stuff in my<br>
_own_ mind. &nbsp;&gt;8-&gt;<br>
<div><div></div><div class="Wj3C7c"><br>
<br>
<br>
<br>
&gt;<br>
&gt; Thanks in advance for answering my RSpec questions! :D<br>
&gt;<br>
&gt; --Tiffani AB<br>
&gt;<br>
&gt;<br>
&gt; 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>
&gt;&gt;<br>
&gt;&gt; On Fri, Jul 4, 2008 at 8:32 AM, Tiffani Ashley Bell<br>
&gt;&gt; &lt;<a href="mailto:tiffani2k3@gmail.com">tiffani2k3@gmail.com</a>&gt; wrote:<br>
&gt;&gt; &gt; Hi everybody,<br>
&gt;&gt;<br>
&gt;&gt; Hi Tiffany, welcome to Rspec<br>
&gt;&gt;<br>
&gt;&gt; &gt; I was reading the Typo source code, however, and came across some code<br>
&gt;&gt; &gt; that<br>
&gt;&gt; &gt; I didn&#39;t know exactly how it worked. &nbsp;I&#39;ve noticed that in testing one<br>
&gt;&gt; &gt; of<br>
&gt;&gt; &gt; their controllers, they use a variable (@comments) that they don&#39;t<br>
&gt;&gt; &gt; declare<br>
&gt;&gt; &gt; anywhere else, yet they use it as a stand in for collections on some of<br>
&gt;&gt; &gt; the<br>
&gt;&gt; &gt; mocks. &nbsp;How is that possible? &nbsp;I know in the mocking documentation it<br>
&gt;&gt; &gt; says<br>
&gt;&gt; &gt; that you can define collaborations with other objects before those<br>
&gt;&gt; &gt; objects<br>
&gt;&gt; &gt; exist, but how is that working in this code? &nbsp;I only ask that because<br>
&gt;&gt; &gt; later,<br>
&gt;&gt; &gt; you see code like this: &nbsp;@comments.stub!(:build).and_return(@comment).<br>
&gt;&gt;<br>
&gt;&gt; If you have a look at the descriptions, they use :shared =&gt; true.<br>
&gt;&gt; This is a way of being DRY in RSpec (which I personally don&#39;t think is<br>
&gt;&gt; such a good idea).<br>
&gt;&gt;<br>
&gt;&gt; What the shared =&gt; true declaration allows you to do is to include<br>
&gt;&gt; that block of code elsewhere with &#39;it should behave like my shared<br>
&gt;&gt; code&#39;<br>
&gt;&gt;<br>
&gt;&gt; So we have (describe &quot;All Requests&quot;, :shared =&gt; true do)<br>
&gt;&gt;<br>
&gt;&gt; and then the next description block is:<br>
&gt;&gt;<br>
&gt;&gt; describe &quot;General Comment Creation&quot;, :shared =&gt; true do<br>
&gt;&gt; &nbsp;it_should_behave_like &quot;All Requests&quot;<br>
&gt;&gt;<br>
&gt;&gt; Which then includes the All Requests block (which is just a before<br>
&gt;&gt; method).<br>
&gt;&gt;<br>
&gt;&gt; The @comments variable gets declared in:<br>
&gt;&gt;<br>
&gt;&gt; @comments.stub!(:build).and_return(@comment)<br>
&gt;&gt;<br>
&gt;&gt; and then this is tied in to the Article model in the _previous_ code<br>
&gt;&gt; block like so:<br>
&gt;&gt;<br>
&gt;&gt; &nbsp; &nbsp;@article &nbsp;= mock_model(Article,<br>
&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:comments &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =&gt; @comments,<br>
&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:published_comments &nbsp; &nbsp; &nbsp; &nbsp; =&gt; @comments,<br>
&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:add_comment &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=&gt; @comment)<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; So when you call @article.comments you get @comments as a stub back<br>
&gt;&gt; which stubs :build and returns a @comment.<br>
&gt;&gt;<br>
&gt;&gt; Ugh.<br>
&gt;&gt;<br>
&gt;&gt; This is where, in RSpec, you can dig a very fast grave. &nbsp;Because<br>
&gt;&gt; you&#39;ll come back to this code in 6-12 months and be totally stuck<br>
&gt;&gt; trying to figure out what is where.<br>
&gt;&gt;<br>
&gt;&gt; I recently wrote a viewpoint on this that might help you:<br>
&gt;&gt;<br>
&gt;&gt; <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>
&gt;&gt;<br>
&gt;&gt; Hope you do well with Rspec, feel free to ask more questions!<br>
&gt;&gt;<br>
&gt;&gt; --<br>
&gt;&gt; <a href="http://lindsaar.net/" target="_blank">http://lindsaar.net/</a><br>
&gt;&gt; Rails, RSpec, Puppet and Life blog....<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; rspec-users mailing list<br>
&gt;&gt; <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>
&gt;&gt; <a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; rspec-users mailing list<br>
&gt; <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>
&gt; <a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>
&gt;<br>
<br>
<br>
<br>
</div></div><font color="#888888">--<br>
Have Fun,<br>
Steve Eley<br>
Deep Salt Team<br>
</font><div><div></div><div class="Wj3C7c">_______________________________________________<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>
</div></div></blockquote></div><br>