Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

xUnit for test DAL in .net core 2 and dependency injection - a little bit of confusion

$
0
0

I a little bit of confusion about xUnit for test my DAL.

My goal is to verify that my DAL correctly accesses the DB and extract the right data.

I create a xUnit test project and try to do a simpli test with Moq like this

[Fact]
    public void Test1()
    {
        // Arrange
        var mockMyClass = new Mock<IMyClassBLL>();

        // Setup a mock stat repository to return some fake data within our target method
        mockStAverageCost.Setup(ac => ac.GetBy(It.IsAny<MyClassVO>())).Returns(new List<MyClassVO>
        {
            new MyClassVO { HCO_ID = "1"},
            new MyClassVO { HCO_ID = "2"},
            new MyClassVO { HCO_ID = "3"},
            new MyClassVO { HCO_ID = "4"}
        });

        // create our MyTest by injecting our mock repository
        var MyTest = new MyClassBLL(mockMyClass.Object);

        // ACT - call our method under test
        var result = MyTest.GetBy();

        // ASSERT - we got the result we expected - our fake data has 6 goals  we should get this back from the method
        Assert.True(result.Count == 4);
    }

The method above work fine.

Now I want access directly to the db for get data.

Obviously something escapes me, I did not understand how to perform a data test with .net core 2 simulating dependeny injection and accessing the data.

Can someone clarify my ideas?


Viewing all articles
Browse latest Browse all 9386

Trending Articles