Testing with Effort and TypeName attribute

c# dbcontext effort entity-framework unit-testing

Question

To test some code, I need to pretend that the DbContext declared in DAL exists.Entity Framework 6 - Code first ). Generally speaking, everything is going okay, except when the data model class uses theTypeName attribute. To illustrate the problem, I constructed a skeleton.

The data structure:

[Table("Customer")]
public class Customer
{
    [Key]
    public int Id { get; set; }

    public string FirstName{ get; set; }

    public string LastName { get; set; }

    //this line causes the exception
    [Column(TypeName = "money")]
    public decimal Salary { get; set; }
}

The concept of context

public class MyDbContext : DbContext
{
    public MyDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
    {
    }

    public MyDbContext(DbConnection connection) : base(connection, true)
    {
    }

    public IDbSet<Customer> Customers { get; set; }
}

And a false context generation technique using certain data:

private MyDbContext GenerateFakeDbContext()
{
    var connection = DbConnectionFactory.CreateTransient();
    var context = new MyDbContext(connection);
    var customer = new Customer
                   {
                       Id = 1,
                       FirstName = "X",
                       LastName = "Y",
                       Salary = 1000
                   };
    //this line throws the exception
    context.Customers.Add(customer);

    return context;
}

The issue is thatTypeName attribute. Tests succeed when it is commented out. However, if it's enabled, I have aSystem.InvalidOperationException hurled, saying

Sequence contains no matching element

In conclusion, my inquiries are:

  • Is it possible for me to utilize Effort in the tests despite the fact that the data model class has attributes?
  • If not, how should I go about making a phony DbContext instead?
1
3
12/14/2015 6:50:45 PM

Accepted Answer

ZZZ_tmp
0
12/13/2015 8:54:24 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow