Forum: RSpec Testing on enter state

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2008-11-19 14:19
(Received via mailing list)
I was reading some old posts on how we shouldn't test private methods,
but there seemed to be an exception for states.

When my model enters a state, a method is executed. (:on => Proc ...)
I just want to test this method.  I couldn't find much material on it.
 How can I go about this?

Thanks,
Ramon Tayag
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-11-19 14:52
(Received via mailing list)
On Wed, Nov 19, 2008 at 7:17 AM, Ramon Tayag <ramon.tayag@gmail.com>
wrote:
> I was reading some old posts on how we shouldn't test private methods,
> but there seemed to be an exception for states.
>
> When my model enters a state, a method is executed. (:on => Proc ...)
> I just want to test this method.  I couldn't find much material on it.
>  How can I go about this?

While there are always exceptions to guidelines, I've never seen a
general exception to "don't test privates" for states.

Are you averse to just going at this from the public API?
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2008-11-19 14:55
(Received via mailing list)
Not sure if I'm understanding right, but no, I can make the method
public -- I just got used to methods, that aren't (or don't need to
be) called by anything but its owner, being private.

If I make it public it solves my question somewhat :)

Thanks,
Ramon Tayag
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-11-19 15:02
(Received via mailing list)
On Wed, Nov 19, 2008 at 7:55 AM, Ramon Tayag <ramon.tayag@gmail.com>
wrote:
>> general exception to "don't test privates" for states.
>>
>> Are you averse to just going at this from the public API?

> Not sure if I'm understanding right, but no, I can make the method
> public -- I just got used to methods, that aren't (or don't need to
> be) called by anything but its owner, being private.

I didn't mean make the methods public. I meant use the public API.

For example, this:

describe Dog do
  it "should increase its heart rate when the postman arrives" do
    dog = Dog.new
    lambda {dog.see Postman.new}.should change{dog.heartrate}.by(10)
  end
end

rather than this:

describe Dog do
  it "should transition to an agitated state when the postman arrives"
do
    dog = Dog.new
    dog.should_receive(:transition_to).with(:agitated)
    dog.see Postman.new
  end
end

Make sense?

>
> If I make it public it solves my question somewhat :)
>
> Thanks,
> Ramon Tayag
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2008-11-19 15:28
(Received via mailing list)
Ahh.. yes, you do.  Thanks a lot!  That definitely answers my question.

Ramon Tayag
Afe1e6b75aace67db4b3ac064256b0f1?d=identicon&s=25 Rahoul Baruah (Guest)
on 2008-11-19 15:30
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On 19 Nov 2008, at 14:01, David Chelimsky wrote:

>
> describe Dog do
>  it "should transition to an agitated state when the postman
> arrives" do
>    dog = Dog.new
>    dog.should_receive(:transition_to).with(:agitated)
>    dog.see Postman.new
>  end
> end


Can I just say that that is a truly great example :-)


Rahoul Baruah
Web design and development: http://www.3hv.co.uk/
Nottingham Forest: http://www.eighteensixtyfive.co.uk/
Serious Rails Hosting: http://www.brightbox.co.uk/
Lifecast: http://www.madeofstone.net/






-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkkkIv4ACgkQu0BNRvjN8xSqWACfRAq1jiaqQnxDfvhdG/F1xy4A
qeMAn11P+WAcY8F7XVENIfMl1BEE9sO5
=Vp0S
-----END PGP SIGNATURE-----
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2008-11-19 15:42
(Received via mailing list)
In other words, the object changes its private state to achieve a
publically
visible result (otherwise, why bother?) Test that result.

///ark
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2008-11-19 19:29
(Received via mailing list)
In other words, the object changes its private state to achieve a
publically
visible result (otherwise, why bother?) Test that result.

///ark
This topic is locked and can not be replied to.