模拟UserManager

时间:2016-05-25 08:49:33

标签: c# asp.net mocking

我使用这篇文章(http://www.asp.net/web-api/overview/testing-and-debugging/mocking-entity-framework-when-unit-testing-aspnet-web-api-2)来模拟数据库并能够进行测试。但是我的一些类将ApplicationUser作为外键。所以我很难测试它们。我该怎么办?

这是我的界面:

using System;
using System.Data.Entity;

namespace Prac.Models
{
   public interface IPracContext : IDisposable
   {
       IDbSet<Graduando> Graduando { get; set; }
       IDbSet<Funcionario> Funcionario { get; set; }
       IDbSet<ApplicationUser> Users { get; set; }

       int SaveChanges();
       void MarkAsModified<T>(T i) where T : class;
    }
}

IdentityModels.cs(带有applicatiouser类)

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace Prac.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IPracContext
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
            //solve problem of json serialization
            //db was getting data from foreign key
            base.Configuration.ProxyCreationEnabled = false;
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

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

        public IDbSet<Beneficio> Beneficio { get; set; }

        public IDbSet<Familiar> Familiar { get; set; }

        public IDbSet<CadastroGraduando> CadastroGraduando { get; set; }

        public IDbSet<Graduando> Graduando { get; set; }

        public IDbSet<DoencaCronica> DoencaCronica { get; set; }

        public IDbSet<Edital> Edital { get; set; }

        public IDbSet<EditalGraduando> EditalGraduandoe { get; set; }

        public IDbSet<Funcionario> Funcionario { get; set; }

        public IDbSet<Noticia> Noticia { get; set; }
    }
}

0 个答案:

没有答案
相关问题