Skip to content

Keboo/NosePlug

Repository files navigation

NosePlug

Sometimes you have to test code that smells. When using static methods it tightly couples two pieces of code together. When over used this can lead to code that is difficult to test since all coupled code will be invoked. Ideally the code should be refactored to eliminate this problem, however in the real world ideals are not always possible. This library serves to be a half way point to allow testing to occur during the interim before appropriate refactoring can occur.

Getting started

This library's API is heavily influenced by Moq. See the documentation for the full getting started guide.

A simple example "mocking" DateTime.Now.

var nowPlug = Nasal.Property(() => DateTime.Now)
      .Returns(() => new DateTime(1987, 4, 20));

using (await Nasal.ApplyAsync(nowPlug))
{
      Assert.Equal(new DateTime(1987, 4, 20), DateTime.Now);
}

Limitations

This library relies on Harmony to monkey patch methods. This library maintains all of the same limitations of Harmony. The most common failure comes from inlining. This is most apparent when compiling in Release configuration (optimizations enabled).