samedi 2 mai 2015

Unit Testing Account controller with ApplicationDbContext

I am a total beginner to unit testing and am having trouble testing some of my methods in the account controller that use the ApplicationDbContext. I have been following this tutorial: http://ift.tt/19HUIeZ
but I cannot seem to access some of the variables related to an applicationUser. In my account controller I am able to access the Id, Username, email etc that are inherited from its superclass "IdentityUser" but for some reason I cant seem to access these variables in my test controller. I have the followig implementation thus far:

interface IStoreAppContext : IDisposable
    {
        DbSet<ApplicationUser> Users { get; }
        int SaveChanges();
        void MarkAsModified(ApplicationUser item);
    } 

My IdentityModel class:

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IStoreAppContext
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

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

        public DbSet<SportProgram> SportContext {get; set;}
        public DbSet<Exercise> ExerciseContext { get; set; }
        public DbSet<ExerciseImage> ExerciseImageContext { get; set; }
        public DbSet<Store> StoreContext { get; set; }


        public void MarkAsModified(ApplicationUser item)
        {
            Entry(item).State = EntityState.Modified;
        }

I have added the following to my AccountController:

 private IStoreAppContext db2 = new ApplicationDbContext();

    // add these contructors
    public AccountController() { }

    public AccountController(IStoreAppContext context)
    {
        db2 = context;
    }

In my test project:

 class TestProductDbSet : TestDbSet<ApplicationUser>
    {
        public override ApplicationUser Find(params object[] keyValues)
        {
            return this.SingleOrDefault(user => user. == (int)keyValues.Single());
        }

The return line above is one of the first places where I have this problem. I have "user => user. " and when visual studio offers my suggestions after the "." all that is given to me from the ApplicationUser class are the variables that I have added and I can't seem to access the ApplicationUser Id, username, email that are inherited from the IdentityUser class. Can anyone help please?

Here is another class in my test project that may help you:

class TestStoreAppContext : IStoreAppContext
    {
        public TestStoreAppContext()
        {
            this.Products = new TestProductDbSet();
        }

        public DbSet<ApplicationUser> Products { get; set; }

        public int SaveChanges()
        {
            return 0;
        }

        public void MarkAsModified(ApplicationUser item) { }
        public void Dispose() { }
    }

Aucun commentaire:

Enregistrer un commentaire