https://github.com/budougumi0617/testable
Allows test code to call methods and properties even if non public.
https://github.com/budougumi0617/testable
csharp dotnet-standard nuget-package unittest
Last synced: about 1 year ago
JSON representation
Allows test code to call methods and properties even if non public.
- Host: GitHub
- URL: https://github.com/budougumi0617/testable
- Owner: budougumi0617
- License: mit
- Created: 2017-07-03T13:55:51.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-13T07:21:18.000Z (over 8 years ago)
- Last Synced: 2025-05-30T08:33:46.650Z (about 1 year ago)
- Topics: csharp, dotnet-standard, nuget-package, unittest
- Language: C#
- Size: 47.9 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Testable
[](https://badge.fury.io/nu/budougumi0617.Testable)
[](https://ci.appveyor.com/project/budougumi0617/testable)
[](https://codecov.io/gh/budougumi0617/Testable)
# Introduction
Allows test code to call fields, methods, ~~and properties~~ even if non public, like a `PrivateObject`.
[Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject](https://msdn.microsoft.com/ja-jp/library/microsoft.visualstudio.testtools.unittesting.privateobject.aspx)
And, you can call static fiealds like a `PrivateType`. but, you do not need create wrapper instance if you want to call static members.
[Microsoft.VisualStudio.TestTools.UnitTesting.PrivateType](https://msdn.microsoft.com/ja-jp/library/microsoft.visualstudio.testtools.unittesting.privatetype.aspx)
# How to use
Easy sample is below. If you want to read more example, please check [test codes](./Testable.Tests/PrivateObjectTests.cs).
```cs
public class TargetClass
{
private int privateInt;
private static privateStaticInt;
static string throughString(string s)
{
return s;
}
}
---
PrivateObject pivateObject = new PrivateObject(new TargetClass() as Object);
int privateIntField = Convert.ToInt32(po.GetField("privateInt"));
po.SetField("privateInt", 1000);
// Access private static field by extension methods.
typeof(TargetClass).SetStaticField("privateStaticInt", 100);
int privateStaticIntField = Convert.ToInt32(typeof(TargetClass).GetStaticField("privateStaticInt"));
typeof(TargetClass).SetStaticField("privateStaticMethod", 100);
// Call private static method by extension methods.
var argTypes = new Type[] { typeof(string) };
var args = new Object[] { "arguments" };
var returnString = (string)typeof(TargetClass).InvokeStatic("throughString", argTypes, args);
```
# Features
## `Testable.PrivateObject`
|Method|Description|
|---|---|
|`PrivateObject(Object)`|Initializes a new instance of the PrivateObject class that creates the wrapper for the specified object.|
|`GetField(String)`|Gets a value from a named field, based on the name.|
|`SetField(String, Object)`|Sets a value for the field of the wrapped object, identified by name.|
|`Invoke(String, Type[], Object[])`|Used to access the methods of the private object.|
|???|Now implementing...|
## `Testable.PrivateStaticAccessor`
|Method|Description|
|---|---|
|`GetStaticField(this Type, string)`|Gets a value from a named static field, based on the name.|
|`SetStaticField(this Type, string, Object)`|Sets a value for a named static field, identified by name.|
|`InvokeStatic(this Type, string, Type[], Object[])`|Invokes static method.|
|???|Now implementing...|