Thanks on the reminder that "new" is a class method. Plus, I figured using mocks and fixtures together was probably a crappy idea. I'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 <<a href="mailto:steve@deepsalt.com">steve@deepsalt.com</a>> 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>
<<a href="mailto:tiffani2k3@gmail.com">tiffani2k3@gmail.com</a>> wrote:<br>
><br>
> When I run the tests the third test fails and RSpec complains that "Mock<br>
> 'Account_1003' expected :new with (any args) once, but received it 0 times"<br>
><br>
> I'm confused about that since I am calling Account.new in the create method<br>
> on the controller. What's really wrong here?<br>
<br>
</div>The problem there is that Account.new is a _class_ method on the<br>
Account class. The @mock_account you made is an _instance_ of Account<br>
(actually, not even that, it's a mock object that will pretend it's an<br>
Account if you ask it). You're not sending @mock_account any<br>
messages, you're sending them to the Account class. 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'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's not a great idea<br>
to do both at the same time. 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's faster and B.)<br>
you're isolating your tests to *just* the controller code, and won't<br>
have to worry about tests failing because the models are broken.<br>
(That's what the model specs are for.) >8-><br>
<br>
I'm also unclear on the relationship between User and Account in this<br>
code, and why you're creating a new account for every new user in the<br>
UsersController... But that's really about your application, not<br>
about RSpec. If that'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. I'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. >8-><br>
<div><div></div><div class="Wj3C7c"><br>
<br>
<br>
<br>
><br>
> Thanks in advance for answering my RSpec questions! :D<br>
><br>
> --Tiffani AB<br>
><br>
><br>
> On Thu, Jul 3, 2008 at 9:09 PM, Mikel Lindsaar <<a href="mailto:raasdnil@gmail.com">raasdnil@gmail.com</a>> wrote:<br>
>><br>
>> On Fri, Jul 4, 2008 at 8:32 AM, Tiffani Ashley Bell<br>
>> <<a href="mailto:tiffani2k3@gmail.com">tiffani2k3@gmail.com</a>> wrote:<br>
>> > Hi everybody,<br>
>><br>
>> Hi Tiffany, welcome to Rspec<br>
>><br>
>> > I was reading the Typo source code, however, and came across some code<br>
>> > that<br>
>> > I didn't know exactly how it worked. I've noticed that in testing one<br>
>> > of<br>
>> > their controllers, they use a variable (@comments) that they don't<br>
>> > declare<br>
>> > anywhere else, yet they use it as a stand in for collections on some of<br>
>> > the<br>
>> > mocks. How is that possible? I know in the mocking documentation it<br>
>> > says<br>
>> > that you can define collaborations with other objects before those<br>
>> > objects<br>
>> > exist, but how is that working in this code? I only ask that because<br>
>> > later,<br>
>> > you see code like this: @comments.stub!(:build).and_return(@comment).<br>
>><br>
>> If you have a look at the descriptions, they use :shared => true.<br>
>> This is a way of being DRY in RSpec (which I personally don't think is<br>
>> such a good idea).<br>
>><br>
>> What the shared => true declaration allows you to do is to include<br>
>> that block of code elsewhere with 'it should behave like my shared<br>
>> code'<br>
>><br>
>> So we have (describe "All Requests", :shared => true do)<br>
>><br>
>> and then the next description block is:<br>
>><br>
>> describe "General Comment Creation", :shared => true do<br>
>> it_should_behave_like "All Requests"<br>
>><br>
>> Which then includes the All Requests block (which is just a before<br>
>> method).<br>
>><br>
>> The @comments variable gets declared in:<br>
>><br>
>> @comments.stub!(:build).and_return(@comment)<br>
>><br>
>> and then this is tied in to the Article model in the _previous_ code<br>
>> block like so:<br>
>><br>
>> @article = mock_model(Article,<br>
>> :comments => @comments,<br>
>> :published_comments => @comments,<br>
>> :add_comment => @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. Because<br>
>> you'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>
>><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>
>><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>
><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" target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>
><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>