Error executing test project

5

I added a test project in my solution to test the methods of my MVC application.

I created a base class, for the other classes with tests to inherit from it, which contains the creation of the context ( IdentityDbContext ).

In the constructor class of the base class I added a breakpoint and in the test project there is only one class of tests, with a method only, and this is inheriting from the base class.

public abstract class BaseTest
{
    protected CustomContext Context;

    protected BaseTest()
    {
        Context = new CustomContext();  // um breakpoint é colocado aqui
    }
}

I put a breakpoint in the constructor of the base class exactly on the first line where you should instantiate the context of the database. But running the debug of the test does not even run this line because it does not stop at breakpoint , and a error is already triggered.

  Managed Debugging Assistant 'DisconnectedContext' has detected a problem in 'C: \ PROGRAM FILES (X86) \ MICROSOFT VISUAL STUDIO 14.0 \ COMMON7 \ IDE \ COMMONEXTENSIONS \ MICROSOFT \ TESTWINDOW \ te.processhost.managed.exe'.

     

The transition to the COM 0xaff5d8 context for this RuntimeCallableWrapper failed with the following error: The calling object was disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED)). Usually this occurs because the COM context 0xaff5d8 where this RuntimeCallableWrapper was created is disconnected or is busy doing something else. Releasing the interfaces from the current COM context (COM context 0xaff468). This can cause corruption or loss of data. To avoid this problem, make sure all COM contexts / apartments / threads stay alive and are available for context transition until the application completely exits RuntimeCallableWrappers that represent COM components residing on them.

In the Unit Test Session window the following message appears:

  

Unable to create instance of class MyProject.SubPasta.HomeControllerTest. Error: System.TypeLoadException: Method Set in type MyOtherProject.Context.CustomContext of assembly MyOtherProject, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null does not have an implementation ..

     

in MyProject.SubPasta.Base.BaseTest..ctor ()        in MyProject.SubPasta.HomeControllerTest..ctor ()

Does anyone know what it's all about and how could I resolve it?

My test class:
In this method, no breakpoint is also triggered:

[TestClass]
public class HomeControllerTest : BaseTest
{
    [TestMethod]
    public void TentativaDeAcessoAoIndexComoAnonimo()
    {
        var usuario = Context.Users
            .SingleOrDefault(x => x.UserName == "Anonimo");

        if (usuario == null)
            throw new Exception(GetType().Name + ": Usuário inválido");

        Assert.AreEqual(usuario.Nome, "Anonimo", "Deu bug!");
    }
}

Context code:

public class CustomContext : IdentityDbContext<Usuario>, IContext
{
    public CustomContext() : base("DefaultConnection")
    {
        Configuration.ProxyCreationEnabled = false;
        Configuration.LazyLoadingEnabled = false;
    }

    public DbSet<Classe> Classes { get; set; }
    ...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

        modelBuilder.Properties<string>().Configure(x => x.HasColumnType("varchar"));

        modelBuilder.Configurations.Add(new ClasseConfiguration());
        ...

        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>().ToTable("Usuarios");
        modelBuilder.Entity<Usuario>().ToTable("Usuarios");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");

        // para não criar o campo IdentityUser_Id
        modelBuilder.Entity<IdentityUser>().HasMany(x => x.Roles)
            .WithRequired()
            .HasForeignKey(x => x.UserId);

        modelBuilder.Entity<IdentityUser>().HasMany(x => x.Claims)
            .WithRequired()
            .HasForeignKey(x => x.UserId);

        modelBuilder.Entity<IdentityUser>().HasMany(x => x.Logins)
            .WithRequired()
            .HasForeignKey(x => x.UserId);
    }

Interface IContext :

public interface IContext
{
    DbChangeTracker ChangeTracker { get; }
    DbContextConfiguration Configuration { get; }
    Database Database { get; }
    IDbSet<IdentityRole> Roles { get; set; }
    IDbSet<Usuario> Users { get; set; }
    void Dispose();
    DbEntityEntry Entry(object entity);
    DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;
    bool Equals(object obj);
    int GetHashCode();
    Type GetType();
    IEnumerable<DbEntityValidationResult> GetValidationErrors();
    int SaveChanges();
    Task<int> SaveChangesAsync();
    Task<int> SaveChangesAsync(CancellationToken cancellationToken);
    DbSet Set(Type entityType);
    DbSet<TEntity> Set<TEntity>() where TEntity : class;
}
    
asked by anonymous 19.11.2015 / 14:25

1 answer

3

The error message says:

  

Unable to create instance of class MyProject.SubPasta.HomeControllerTest. Error: System.TypeLoadException: Set method in type MyOtherProject.Context.CustomContext of assembly MyOtherProject, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null does not have an implementation.

And its IContext interface has the following:

public interface IContext
{
    ...
    DbSet<TEntity> Set<TEntity>() where TEntity : class;
}

The error says that this method is not implemented in your class. So the mistake.

There are several ways to solve. I would take the context class interface first and test without it. Then I would make the adjustments to class work again with the interface.

    
19.11.2015 / 16:02