Popular Posts

Monday, June 27, 2016

Passing data from controller to view mb

http://www.codeproject.com/Articles/1108855/ways-to-Bind-Multiple-Models-on-a-View-in-MVC

Tuesday, June 21, 2016

Testing, generating data automatically using AutoFixture/AutoFixture: AutoFixture is an open source library for .NET designed to minimize the 'Arrange' phase of your unit tests in order to maximize maintainability. Its primary goal is to allow developers to focus on what is being tested rather than how to setup the test scenario, by making it easier to create object graphs containing test data.

GitHub - AutoFixture/AutoFixture: AutoFixture is an open source library for .NET designed to minimize the 'Arrange' phase of your unit tests in order to maximize maintainability. Its primary goal is to allow developers to focus on what is being tested rather than how to setup the test scenario, by making it easier to create object graphs containing test data.



Testing, mocking and autofixture

Testing Entity framework using Moq and extension method

Moq extension to transform a list to Moq of DBSet for testing the Entity Framework DbSet.


public static class Extensions
{
    public static Mock<DbSet<T>> GetMockSet<T>(this IQueryable<T> data) where T : class
    {
        var mockSet = new Mock<DbSet<T>>();
        mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(data.Provider);
        mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(data.Expression);
        mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(data.ElementType);
        mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns<T>(x => data.GetEnumerator());
 
        return mockSet;
    }
}

To use it in the test as following:

[TestMethod]
public void GetCustomer_Mocked_With_Extension()
{
    var mockedContext = new Mock<IDbContext>();
 
    var data = new List<Customer>
    {
        new Customer { FirstName = "Rahman", LastName = "Mahmoodi", Id = 1 }
    }.AsQueryable();
 
    var mockSet = data.GetMockSet<Customer>(); // This is how to use it
 
    mockedContext.Setup(x => x.Customers).Returns(mockSet.Object);
 
    var cbo = new CustomerBusinessObject(mockedContext.Object);
    var c = cbo.Get(1);
    Assert.IsTrue(c.FirstName == "Rahman");
 
}

Entity Framework Testing with a Mocking Framework (EF6 onwards)

Entity Framework Testing with a Mocking Framework (EF6 onwards)

Friday, June 17, 2016

Populating combos from enum

http://www.codeproject.com/Articles/1105885/Populating-Combo-Boxes-from-Enums