[rspec-users] specs on private methods

Francois Wurmus francois.wurmus at infopark.de
Thu Jan 10 15:21:17 EST 2008

Apart from private or public methods I see a problem in your test case.
You don't make sure that your mac_address is returned in exactly the way 
you want - you are merely saying it should look like the return value of 
some (protected) method you call (normalize_mac).

Let's assume this method was implemented wrong and just returns nil. 
Let's further assume that @signup.mac_address calls a reader method 
returning nil, too. Such an implementation, though wrong, would satisfy 
your spec!

A second problem is, that the parameter you are passing to the 
normalize-method will already have been transformed before your method 
gets its hands on it. You are not passing the init value but the value 
that is returned be a read operation on the mac_address field.

What you really should specify is your concrete mac address format:

    it "should return a valid mac address" do
       @signup.mac_address = "00-11-22-33-44-55"
       @signup.mac_address.should == "00:11:22:33:44:55"

Now you are not only implementation independent but your spec is also 
saying more.


barsalou schrieb:
> It's interesting that this thread has started because I just ran into 
> this problem.
> The error I got was:
> NoMethodError in 'Signup she be a valid mac address'
> protected method `normalize_mac' called for #<Signup:0x408c0434>
> ./spec/models/signup_spec.rb:10:
> Here's the spec:
> describe Signup do
>   before(:each) do
>     @signup = Signup.new
>   end
>   it "she be a valid mac address" do
>      @signup.mac_address = "00-11-22-33-44-55-66"
>      normalized = @signup.normalize_mac(@signup.mac_address)
>      @signup.mac_address.should == normalized
>   end
> end
> I have a model that has mac_address attribute.  In the before 
> validation, I wanted to make the mac address have colons(:) instead of 
> whatever the user typed in which could have spaces between, dashes, 
> nothing at all.
> My thought was to just remove all of those special characters validate 
> it against a regex then if it passed the regex check produce the mac 
> address with the colons included.
> And, if I understand it properly, Pat's statement is saying that I 
> really shouldn't be protecting that method...which would fix my problem.
> So my question is then, how do you know when to use protected and 
> private or do I just do what Rick suggested and use send?
> I just realized this isn't really an rspec question...so I'll just move along.
> Mike B.
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list