Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Supports replacing public and private instance methods, static methods and properties without any profiler.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 MReplace
Octocat-spinner-32 MethodTests
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Local.testsettings
Octocat-spinner-32 MethodReplacer.sln
Octocat-spinner-32 MethodReplacer.vsmdi
Octocat-spinner-32 README.md
Octocat-spinner-32 TraceAndTestImpact.testsettings
README.md

MReplace

Supports replacing public and private instance methods, static methods and properties without any profiler. I use Ziad Elmalki's technique for the replacing of two MethodBase objects (http://www.codeproject.com/Articles/37549/CLR-Injection-Runtime-Method-Replacer).

[TestMethod]
public void Replace_StaticMethod()
{
    Assert.AreEqual(2, ClassUnderTest2.StaticMethod2());
    using (Replace.Method(() => ClassUnderTest2.StaticMethod2()).With(() => ClassUnderTest1.StaticMethod1()))
    {
        Assert.AreEqual(1, ClassUnderTest2.StaticMethod2());
    }
    Assert.AreEqual(2, ClassUnderTest2.StaticMethod2());
}

[TestMethod]
public void Replace_InstanceMethod()
{
    var tc = new ClassUnderTest2();
    Assert.AreEqual(2, tc.Method2());
    using (Replace.Method<ClassUnderTest2>(c => c.Method2()).With<ClassUnderTest1>(c => c.Method1()))
    {
        Assert.AreEqual(1, tc.Method2());
    }
    Assert.AreEqual(2, tc.Method2());
}

[TestMethod]
public void Replace_PrivateInstanceMethod()
{
    var tc = new ClassUnderTest2();
    Assert.AreEqual(2, tc.CallPrivateMethod2());
    using (Replace.Method<ClassUnderTest2>("PrivateMethod2").With<ClassUnderTest1>("PrivateMethod1"))
    {
        Assert.AreEqual(1, tc.CallPrivateMethod2());
    }
    Assert.AreEqual(2, tc.CallPrivateMethod2());
}

[TestMethod]
public void Replace_StaticProperty()
{
    Assert.AreEqual(2, ClassUnderTest2.StaticProperty2);
    using (Replace.Property(() => ClassUnderTest2.StaticProperty2).With(() => ClassUnderTest1.StaticProperty1))
    {
        Assert.AreEqual(1, ClassUnderTest2.StaticProperty2);
    }
    Assert.AreEqual(2, ClassUnderTest2.StaticProperty2);
}

[TestMethod]
public void Replace_InstanceProperty()
{
    var tc = new ClassUnderTest2();
    Assert.AreEqual(2, tc.Property2);
    using (Replace.Property<ClassUnderTest2>(c => c.Property2).With<ClassUnderTest1>(c => c.Property1))
    {
        Assert.AreEqual(1, tc.Property2);
    }
    Assert.AreEqual(2, tc.Property2);
}
Something went wrong with that request. Please try again.