Unit testing of a class inheriting from an abstract class

My problem is that I want to stub a property in my abstract class, because my class uses this property in the test. I am currently using the latest version of Moq.

My abstract class is as follows:

public abstract class BaseService
{
    protected IDrawingSystemUow Uow { get; set; }
}

And my class in the test looks like this:

public class UserService : BaseService, IUserService
{
    public bool UserExists(Model model)
    {
        var user = this.Uow.Users.Find(model.Id);
        if(user == null) { return false; }

        reurn true;
    }
}

I can’t understand how I can drown the property Uow. Does anyone have a key? Or is my design so bad that I need to go to the property Uowfor my class in the test?

+5
source share
2 answers

. Uow , Moq. , .

- . :

public abstract class BaseService
{
    protected virtual IDrawingSystemUow Uow { get; set; }
}

, Moq ( using Moq.Protected ):

// at the top of the file
using Moq.Protected;

// ...

var drawingSystemStub = new Mock<IDrawingSystemUow>();
var testedClass = new Mock<UserService>();
testedClass 
  .Protected()
  .Setup<IDrawingSystemUow>("Uow")
  .Returns(drawingSystemStub.Object);

// setup drawingSystemStub as any other stub

// exercise test
var result = testedClass.Object.UserExists(...);
+9

, . Uow, IDrawingSystemUow. , IDrawingSystemUow, UserService Uow (, UserExists).

0
source

All Articles