user1021347
user1021347

Reputation: 13

spec'ing private methods

I have some private methods in my rails models. I want to spec it out seperately (use rspec).

I do something like this

  class TestModelA < ModelA
    def public_wrapper_method_A
      private_method_A_from_ModelA
    end
  end 

and I write specs for TestModelA#public_wrapper_method_A

I think there is good case for writing specs for private methods.

What is the best way of writing tests for these private methods.

what do you guys think ? pros/cons?

Upvotes: 0

Views: 118

Answers (2)

patrickmcgraw
patrickmcgraw

Reputation: 2495

I usually set aside a context group for private methods and then setup a describes block for each method. Within the block I define a helper method to call the private method and then the it blocks use the call_xxx method to get to the private method.

context "private methods" do
  describe "#some_private_method" do
    def call_some_private_method
      some_obj.send(:some_private_method)
    end

    it "should return 'something'" do
      call_some_private_method.should == 'something'
    end
  end
end

Upvotes: 1

jdl
jdl

Reputation: 17790

You don't have to wrap your class to test private methods. You can use send instead.

object.send(:foo_private_method)

Upvotes: 0

Related Questions