Testing Private Method using Pex - pex

I want to use Pex and Moles in my project.I want to test Private method using Pex.
Is anyone used Pex to test private method?
If we can't test directly, Is there any workaround to test Private method?

This has been answered by Peli, one of Pex's authors: PEX will not work for private and protected methods and workaround pex internal class.
A few of the alternatives:
Don't test private methods.
Re-design the logic you are testing to increase testability.
Use InternalsVisibleToAttribute. Put [assembly: InternalsVisibleTo("Company.Product.ProjectUnderTest.Tests")] into Company.Product.ProjectUnderTest\Properties\AssemblyInfo.cs.
testing .net

Related

How do I test private/ protected methods in JUnit5?

Is there a way to test private/ protected methods in JUnit5?
In JUnit 4 testing private methods may be an indication that those methods should be moved into another class to promote reusability. And for protected methods tests should be placed in the same package as the classes under test.
What is the case in JUnit5?
In JUnit 4 testing private methods may be an indication that those methods should be moved into another class to promote reusability.
I’d argue that this statement holds without the first three words. There’s no difference between JUnit 4 and 5 in this regard; even more, the testing framework has nothing to do with it at all.
So I recommend that you proceed equally: Either test the private method indirectly through the public interface or extract it to a place where it can be exercised independently.

Can I change a private methods visibility in order to unit test them

I see in this answer that for Java you can set the visibility of a private method to "true" in a unit test in order to test the method. Is there something like this available for VBA, so that I can unit test private methods using RD-VBA?
If not, and I have a class that works out some logic in three private methods and give it back to a return value, am I doomed to only give a input value and test the return value, without being able to test the three private methods doing the heave lifting in between?
You shouldn't need to write tests for private methods, regardless of the language. You test your public API, what's private is implementation detail that isn't relevant.
If it is relevant and important enough to be tested on its own, then you should extract that private method to another class, and expose it as a public member of that class.
For example once I had a form and I wanted to limit user input in a textbox to numeric characters, and since I'm reusing that logic elsewhere then instead of treating it as an implementation detail of my form, I extracted a AsciiInputValidator class, and its public IsValidNumericValue method could be tested in every possible way as its own SUT.
Don't test private methods: the public ones invoke them anyway.
Unfortunately the Extract Class refactoring feature is not implemented as of this writing, so for now Rubberduck can't do this automatically for you... but it's definitely in-scope and if you're reading this and you're up for a bit of a C# metaprogramming challenge, go for it, pull requests are always welcome!
Can you add a public wrapper like
public sub testPrivateSub(param1,param2...)
PrivateSub(param1,param2....)
end sub
private sub PrivateSub(param1,param2....)
....
end sub

How to unit test a non-public method of a class in objective-c?

I have defined a function in a category extension of my class.
Although this function is not a public API, it is a important function and i want to be able to unit test this function.
In the unit test, as expected, XCode complaints the function is not declared.
Is there any way I can get around this?
Generally you should only test interfaces, so maybe the method should actually be public. If you still want the method to be private and write a unit test for it, you can redeclare the method in a category:
Just before your test class add:
#interface ClassUnderTest (IReallyWantToTestPrivateMethodsEvenThoughIShouldnt)
- (void)thePrivateMethodToBeTested;
#end
Don't test private methods. Test the public methods that use the private method. And if the private method doesn't work, the test for the public method that uses it should fail. How a method works (and what private method it calls) is a trivial implementation detail that your test should not be concerned about. Test the result, not how it achieves that result.
See this thread for more about why: https://softwareengineering.stackexchange.com/questions/100959/how-do-you-unit-test-private-methods (They talk about Java, but the principle applies to any language).
Make that function public but put the declaration within #ifdef DEBUG so that it is public only when testing. The released code won't have it as public.

How can I do unit testing for private function in Visual Studio (VB.Net and C#)?

I know it may sound like a silly question as I've heard many saying that you should only do unit testing for public function and indeed, visual basic.net in Visual studio IDE only allows me to do unit testing for public function.
However, I got a public function that is calling to many other private functions.
If I do unit testing for that public function, that would be too complicated. I only want to test each private function individually to make sure it works correctly first, before jumping to the parent public function.
One solution I've had in my mind is that I could change all private functions to public ones so that Visual Studio allows me to do unit testing for them. But it is annoying me as I don't want them to be public.
Is there any suggestions you could let me know please?
many thanks in advance.
N.T.C
If you really can't break the code out into separate classes, you could change all of the private functions to be protected and then create a private class within your test class that inherits from the class you're trying to test (this would be named as a fake or stub, hence my advice to make it private. You don't want code outside of the test class to interact with this). Within your inherited class, create public functions for each of the now protected functions that simply call through to the base and write your unit tests against those instead.
I apologize if this capability is not available in VB:
Create a sub-class of the class you want to test. Ensure that the sub-class has public interfaces to the private functions.
As for "only unit test public functions?" That's horse manure. You test what might fail. For instance, you might have a class with only one public function, and you want to refactor into a set of calls on private functions to decrease the complexity. If you have to refactor your solution for any reason (as one of the comments suggested), then the first step is to have all the pieces of the solution tested that you will have to change during the refactoring.

Modify Property Code At Runtime in VB.NET

Let say I have the following classes,
Public Class Base
Public Event PropertyChanged()
Sub New()
.....
End Sub
End Class
Public Class Other
Inherits Base
Public Property X As String
End Class
If I wanted to say, alter the code definition of the automatic property X (at runtime, perhaps in Sub New), such that I could throw the PropertyChanged event, how could I go about doing that?
Edit: Its seems that the Postsharp is an available tool that does the functionality I'm exploring, but I wanted my question to be more academic than practical. So, how could I implement the code injection at compile time or runtime myself?
I guess you are looking for AOP. A very nice technology that IMHO isn't mature on the dotnet platform. I believe, correct me if I am wrong, that Postsharp is the best known AOP framework. It isn't gratis though for production; installing and playing (and possibly F/OSS) is gratis. Also check this post for more info.
The answer is simple: you can't. Once a type is loaded, you can't change its code.
If you want to implement INotifyPropertyChanged without writing the same code for each property, there are several ways.
One of them is making the property MustOverride (abstract in C#) and then creating another class at runtime which implements it at runtime (for example using Castle DynamicProxy).
Another one is using AOP to rewrite the code after compilation (but before it's run) using something like PostSharp.
Also have a look at Implementing INotifyPropertyChanged - does a better way exist?, for an overview of other options.