[rspec-users] Spec-ing private method

12 views
Skip to first unread message

Andrey S.

unread,
May 15, 2013, 11:22:28 PM5/15/13
to rspec...@rubyforge.org
Yes, I know, that testing private methods it's not a good idea (and I
read this thread - http://www.ruby-forum.com/topic/197346 - and some
others)

But how can I test the following code?
I use xmpp4r. In my public method #listen I start receive jabber
messages like so:

def listen
@client.add_message_callback do |m|
do_things_with_message(m)
end
end

private
def do_things_with_message(m)
#
end

#add_message_callback - runs block, when message come

So, testing #listen method it's difficult and it more testing xmpp4r
than my #do_things_with_message

How to do all right and test #do_things_with_message? :)
(http://www.ruby-forum.com/topic/197346#859664)

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Andrew Premdas

unread,
May 16, 2013, 4:26:59 AM5/16/13
to rspec...@rubyforge.org
On 16 May 2013, at 04:22, "Andrey S." <li...@ruby-forum.com> wrote:
> Yes, I know, that testing private methods it's not a good idea (and I
> read this thread - http://www.ruby-forum.com/topic/197346 - and some
> others)
>
> But how can I test the following code?
> I use xmpp4r. In my public method #listen I start receive jabber
> messages like so:
>
> def listen
> @client.add_message_callback do |m|
> do_things_with_message(m)
> end
> end
>
> private
> def do_things_with_message(m)
> #
> end
>
> #add_message_callback - runs block, when message come
>
> So, testing #listen method it's difficult and it more testing xmpp4r
> than my #do_things_with_message
>
> How to do all right and test #do_things_with_message? :)
> (http://www.ruby-forum.com/topic/197346#859664)


A rule of thumb is that if you really want to test the private method of an object, it probably should be a public method of another object.

Matt Wynne

unread,
May 16, 2013, 5:57:51 PM5/16/13
to rspec...@rubyforge.org
On 16 May 2013, at 04:22, Andrey S. <li...@ruby-forum.com> wrote:

Yes, I know, that testing private methods it's not a good idea (and I
read this thread - http://www.ruby-forum.com/topic/197346 - and some
others)

But how can I test the following code?
I use xmpp4r. In my public method #listen I start receive jabber
messages like so:

def listen
 @client.add_message_callback do |m|
   do_things_with_message(m)
 end
end

private
def do_things_with_message(m)
 #
end

#add_message_callback - runs block, when message come

So, testing #listen method it's difficult and it more testing xmpp4r
than my #do_things_with_message

How to do all right and test #do_things_with_message? :)
(http://www.ruby-forum.com/topic/197346#859664)

What does do_things_with_message do? How would I tell, from the outside of this class, that it has been invoked, and whether it has behaved as expected?

Is there a collaborator on this class that it will send a message to?

Is there some state that it will set on this class that I can read?


Reply all
Reply to author
Forward
0 new messages