Skip to content

A way to automatically mock constructor arguments for a class

License

Notifications You must be signed in to change notification settings

magkal/autonomous-mocker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🤖 Autonomous Mocker

A very lightweight extension the popular C# Moq library, to be able automatically setup your test subjects. Allowing you to write tests faster with less code and more readability. Automatically mocking the parameters if a certain class's constructor parameters.

Todo list

  • Auto mock constructor parameters
  • Be able to pass in configuration arguments to setup mocks and concrete implementations
  • Be able to configure the Moq.Mock Behavior

🎬 Getting started

Examples

Some examples with the imaginary Golfer class.

[Fact]
public void Golfer_should_swing_with_given_club()
{
    var subject = AutoMock.Create<Golfer>();

    var result = subject.Instance.Swing(); 

    Assert.Equal("I swung a mocked club", result);
}

[Fact]
public void Golfer_should_use_concrete_caddy()
{
    var subject = AutoMock.Create<Golfer>(config => 
    {
        config.For<ICaddy>(new Caddy());
    });

    var result = subject.Instance.Swing();

    Assert.Equal("I swung a real club", result);
}

[Fact]
public void Golfer_should_use_the_club()
{
    var subject = AutoMock.Create<Golfer>();
    
    // .GetMock<TType>() is a built in helper function
    // to get a mocked constructor argument
    var clubMock = subject.GetMock<IClub>();

    var result = subject.Instance.Swing();
    
    clubMock.Verify(club => club.Use(), Times.Once);
}

About

A way to automatically mock constructor arguments for a class

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages