RAILS中如何测试 private method. ( how to test private methods in Rails. )

访问量: 1350

modify your spec_helper.rb and add the code below:

#  for testing the private method.
#  EXAMPLE:
#  describe_private User do
#    describe "about the role-permission" do
#      it "should ..." do
#        true.should == true
#      end
#    end
#  end
#  p.s. for another approach, please use: myobject.send(:method_name, args)
#  see:
#  http://stackoverflow.com/questions/267237/whats-the-best-way-to-unit-test-protected-private-met
def describe_private *args, &block
  example = describe *args, &block
  clazz = args[0]
  if clazz.is_a? Class
    saved_private_instance_methods = clazz.private_instance_methods
    example.before do
      clazz.class_eval { public *saved_private_instance_methods }
    end 
    example.after do
      clazz.class_eval { private *saved_private_instance_methods }
    end 
  end 
end

refer to: http://sg552.iteye.com/blog/758130

订阅/RSS Feed

Subscribe

分类/category