Giter Site home page Giter Site logo

Overmock

DOTNET Build

Overmock is a mocking framework in development that allows for creating dynamic proxies that monitor and control expected behavior when writing unit tests. Here are some examples below.

The current goal is refactoring out the dynamic proxy creation into it's own class library to be used by the testing framework.

[TestClass]
public class ExampleTestsForReadMe
{
    public class Model
    {
        public int Id { get; set; }
    }
    public interface IRepository
    {
        bool Save(Model model);
    }
    public interface ILog
    {
        void Log(string message);
    }
    public class Service
    {
        private readonly ILog _log;
        private readonly IRepository _repo;
        public Service(ILog log, IRepository repo)
        {
            _log = log;
            _repo = repo;
        }
        public Model SaveModel(Model model)
        {
            try
            {
                var saved = _repo.Save(model);
                if (!saved)
                {
                    _log.Log("Failed to save");
                }
                return model;
            }
            catch (Exception ex)
            {
                _log.Log(ex.Message);
                throw;
            }
        }
    }

    [TestMethod]
    public void CallsSaveTest()
    {
        var id = 22;
        var wasSaved = false;
        var log = Overmock.AnyInvocation<ILog>();
        var repository = Overmock.Mock<IRepository>()
            .Mock(r => r.Save(Its.Any<Model>()))
            .ToCall(c => {
                wasSaved = true;
                return c.Get<Model>("model")?.Id == id;
            }, Times.Once);

        var service = new Service(log, repository.Target);
        service.SaveModel(new Model { Id = id });

        Assert.IsTrue(wasSaved);
    }

    [TestMethod]
    public void ThrowsExceptionWhenSaveFailsTest()
    {
        var expected = "Failed to save";
        var log = Overmock.AnyInvocation<ILog>();
        var repository = Overmock.Mock<IRepository>();
        Overmock.Mock(repository, r => r.Save(Its.Any<Model>()))
            .ToThrow(new Exception(expected));

        var service = new Service(log, repository.Target);

        try
        {
            service.SaveModel(new Model());

            Assert.Fail("SaveModel Failed to throw an exception.");
        }
        catch (Exception actual)
        {
            Assert.AreEqual(expected, actual.Message);
        }
    }
}

Overmock's Projects

Overmock doesnโ€™t have any public repositories yet.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.