--
You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
To post to this group, send email to rails-...@googlegroups.com.
To unsubscribe from this group, send email to rails-oceani...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.
I'd recommend against testing whether a private method has been called. I treat private methods as the black box internals of the class - you should be able to refactor them without any tests breaking.
What I'd more be looking to test is what I would expect those private methods to do. Are they sending an email? Setting an attribute? What's the (public) behaviour you want to see?
Cheers
--
Pat
And that's the question that, if everyone doing BDD asked themselves
before writing specs, we'd have more BDD and less test-nutty driven
development.
ps: no stab meant at anyone! :)
I tend not to write assertions around private methods, rather to test the public API they should manipulate, and design your private methods in a way which they can be refactored (hopefully) without breaking the public API.
If you're trying to assert this private method getting called, you might be better off actually testing that it's done it's job correctly after being called.
- Ivan
On 22/11/2010, at 9:46 AM, Gabe Hollombe wrote:
This is what I have done previously, and it works great.
As others have said though, you might want to re-think why you need to test this directly, as opposed to observing the behaviour this changes in your class.
From https://github.com/gabehollombe/DirtyMUD/blob/master/spec/dirtymud/entity_spec.rb
it 'listens to server ticks when it is created'
#I'd like to test that Entity#observe(server) is called when I call
Entity.new(:server => server), but how?
The Entity class file:
https://github.com/gabehollombe/DirtyMUD/blob/master/lib/dirtymud/entity.rb
The Responder module that Entity mixes in to give it #observe:
https://github.com/gabehollombe/DirtyMUD/blob/master/lib/dirtymud/responder.rb
#Observer::observe
def observe(obj, do_observe=true)
if do_observe
obj.add_observer(self)
else
obj.delete_observer(self)
end
end
What I'd like to do is test that when I call Entity.new(:server =>
server), the new Entity that's created calls observe(server). Am I
still thinking about this the wrong way?
Clifford Heath.
On 22/11/2010, at 10:11 AM, Gabe Hollombe wrote:
> Let's look at a real-world example instead of my contrived Person with
> private #do_something crappy example.
>
> From https://github.com/gabehollombe/DirtyMUD/blob/master/spec/dirtymud/entity_spec.rb
> it 'listens to server ticks when it is created'
> #I'd like to test that Entity#observe(server) is called when I call
> Entity.new(:server => server), but how?
>
>
> The Entity class file:
> https://github.com/gabehollombe/DirtyMUD/blob/master/lib/dirtymud/entity.rb
> The Responder module that Entity mixes in to give it #observe:
> https://github.com/gabehollombe/DirtyMUD/blob/master/lib/dirtymud/responder.rb
> #Observer::observe
> def observe(obj, do_observe=true)
> if do_observe
> obj.add_observer(self)
> else
> obj.delete_observer(self)
> end
> end
>
> What I'd like to do is test that when I call Entity.new(:server =>
> server), the new Entity that's created calls observe(server). Am I
> still thinking about this the wrong way?
>
>
> On Mon, Nov 22, 2010 at 6:55 AM, Pat Allan <pat@freelancing-
And if it's different for different situations, then you've got more specs to write :)
--
Pat
I see/agree that if I have a spec where I have a server fire off a
tick_event, and make sure that the Entity is handling that properly,
then it eliminates the need for having to assert that
Entity#initialize ever called observe(server) since I'll be testing
the ultimate outcome of the server tick and not the lower unit-level
of the Entity class's implementation. Fair enough, I'll just go about
it that way.
Thanks everyone, as usual, for your feedback. I <3 you guys.
-g
>> On Mon, Nov 22, 2010 at 6:55 AM, Pat Allan <p...@freelancing-gods.com>
And, yes, I know I probably don't want to be creating real Server
instances in my Entity spec. Plenty of refactoring to be done. It's
part of the reason why it's called DirtyMUD, for now. =-D
-g
--