使用SaveChanges上的验证模拟EF6

时间:2015-10-16 16:29:39

标签: c# asp.net entity-framework unit-testing moq

我发现了一篇不错的文章,让我开始使用Moq对基于实体框架的应用程序进行单元测试:https://msdn.microsoft.com/en-us/data/dn314429.aspx

我遇到的这个问题是Mock的SaveChanges方法似乎没有像通常那样触发ValidateEntity方法。我在EntityTypeConfiguration中配置的验证设置均未作为DbEntityValidationException投放。

例如,我的AddRoles_Fails_For_Empty_Name测试以确保该服务无法添加具有空名称的角色。未应用IsRequired()配置,或未调用ValidateEntity方法。我应该提一下,如果我使用网络应用程序中的实际上下文,它可以正常工作。

我已经在下面提供了一些相关的单元测试,DbContext和服务代码。

我做错了什么吗?有任何已知问题或解决方法吗?

角色数据库地图

public class RoleMap : EntityTypeConfiguration<Role>
{
    public RoleMap()
    {
        ToTable("bm_Roles");
        HasKey(r => r.Id);
        Property(r => r.Name).IsRequired().HasMaxLength(100).HasIndex(new IndexAttribute("UX_Role_Name") { IsUnique = true });
        Property(r => r.Description).HasMaxLength(500);
    }
}

的DbContext

public class BlueMoonContext : DbContext, IBlueMoonContext
{
    public BlueMoonContext() : base("name=BlueMoon")
    {

    }

    public DbSet<Role> Roles { get; set; }
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Configurations.AddFromAssembly(typeof(BlueMoonContext).Assembly);
    }

    public void MarkAsModified<T>(T entity) where T : class
    {
        entity.ThrowIfNull("entity");
        Entry<T>(entity).State = EntityState.Modified;
    }

    protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
    {
        var result = base.ValidateEntity(entityEntry, items);

        if (entityEntry.State == EntityState.Added || entityEntry.State == EntityState.Modified)
        {
            // Perform validations that require database lookups
            if (entityEntry.Entity is Role)
            {
                ValidateRole((Role)entityEntry.Entity, result);
            }
            else if (entityEntry.Entity is User)
            {
                ValidateUser((User)entityEntry.Entity, result);                    
            }
        }

        return result;
    }

    private void ValidateRole(Role role, DbEntityValidationResult result)
    {
        if (role.Name.HasValue() && !Roles.NameAvailable(role.Name, role.Id))
        {
            result.ValidationErrors.Add(new DbValidationError("Name", "Already in use"));
        }
    }

    private void ValidateUser(User user, DbEntityValidationResult result)
    {
        if (user.UserName.HasValue() && !Users.UserNameAvailable(user.UserName, user.Id))
        {
            result.ValidationErrors.Add(new DbValidationError("UserName", "Already in use"));
        }
        if (user.Email.HasValue() && !Users.UserNameAvailable(user.UserName, user.Id))
        {
            result.ValidationErrors.Add(new DbValidationError("Email", "Already in use"));
        }
    }
}

帐户服务

public class AccountService : BaseService, IAccountService
{
    private IPasswordHasher _passwordHasher;

    public AccountService(IBlueMoonContext context, IPasswordHasher passwordHasher) : base(context)
    {
        _passwordHasher = passwordHasher;
    }

    public ServiceResult CreateRole(Role role)
    {
        role.ThrowIfNull("role");
        Context.Roles.Add(role);
        return Save();
    }

    // Copied from base service class
    protected ServiceResult Save()
    {
        var result = new ServiceResult();
        try
        {
            Context.SaveChanges();
        }
        catch (DbEntityValidationException validationException)
        {

            foreach (var validationError in validationException.EntityValidationErrors)
            {
                foreach (var error in validationError.ValidationErrors)
                {
                    result.AddError(error.ErrorMessage, error.PropertyName);
                }
            }
        }
        return result;
    }
}

单元测试

[TestFixture]
public class AccountServiceTests : BaseTest
{
    protected Mock<MockBlueMoonContext> _context;
    private IAccountService _accountService;

    [TestFixtureSetUp]
    public void Setup()
    {
        _context = new Mock<BlueMoonContext>();

        var data = new List<Role>
        {
            new Role { Id = 1, Name = "Super Admin" },
            new Role { Id = 2, Name = "Catalog Admin" },
            new Role { Id = 3, Name = "Order Admin" }
        }.AsQueryable();

        var roleSet = CreateMockSet<Role>(data);
        roleSet.Setup(m => m.Find(It.IsAny<object[]>())).Returns<object[]>(ids => data.FirstOrDefault(d => d.Id == (int)ids[0]));

        _context.Setup(m => m.Roles).Returns(roleSet.Object);
        // _context.Setup(m => m.SaveChanges()).Returns(0);
        _accountService = new AccountService(_context.Object, new CryptoPasswordHasher());
    }

    [Test]
    public void AddRole_Fails_For_Empty_Name()
    {
        var role = new Role { Id = 4, Name = "" };

        var result = _accountService.CreateRole(role);
        Assert.False(result.Success);
    }
}

1 个答案:

答案 0 :(得分:3)

SaveChangesvirtual方法,这意味着您调用假方法....

你可以创建你的模拟CallBase = true,但这不是一个好主意(它错过了UT的想法):

_context = new Mock<BlueMoonContext>(){ CallBase = true };

上面的代码将使用BlueMoonContext的实际实现来表示未明确设置的任何方法/属性。

RoleMap负责您的数据库结构,您应该将其作为集成测试(使用DB)的一部分进行测试。

在我看来,您应该创建一个集成测试来验证数据库的完整性(例如;覆盖RoleMap),并使用Throw设置创建一个UT以覆盖catch部分(它& #39; 单元的一部分):

_contest.Setup(x => x.SaveChanges())
          .Throws(new DbEntityValidationException());

编辑以回答评论中的OP问题

不,你不必分开内置验证,你必须创建另一个测试(集成测试)。在此测试中,您将验证验证行为:插入非法实体,期望异常将引发(使用ExpectedExceptionAttribute),然后验证数据库是否为空......要应用此行为,请使用以下模式:

try
{
    \\...
    \\try to commit
}
catch(DbEntityValidationException ex)
{
    \\do some validation, then: 
    throw;\\for ExpectedExceptionAttribute
}

我查看了EntityTypeConfiguration的api,我没有看到任何允许违反规则的联系人(除非您使用MsFakesTypeMock Isolator这样的工具,否则没有验证调用ToTable/HasKey/Property的方法。该课程正在EntityFramework(这是BCL的一部分)中使用,在您不必验证EntityFramework正常工作的集成测试中,您将验证您的自定义规则已集成并按预期工作(在this answer中,您可以阅读不测试BCL类的原因。)

所以在Moq的UT中使用AccountService。为BlueMoonContextRoleMap创建集成测试(不含Moq)。

顺便提一下,@ LadislavMrnka提供interesting way to test(integration test) EntityTypeConfiguration

相关问题