randomThought
randomThought

Reputation: 6393

testing ASP.net MVC 3 business/data logic

What is the best way to test data access layers and business logic in mvc 3 solutions?

I currently have a project where I am using repository classes to access databases, which in turn use hibernate. When I try to create a unit test for them in the auto generated unit tests, they always fail since the configuration for nhibernate is in web.config and and it doesnt try to look there. What am i doing wrong? This particular method returns this error

"The Web request 'http://localhost:35601/' completed 
successfully without running the test"

The test methods look like this

    [TestMethod()]
    [HostType("ASP.NET")]
    [AspNetDevelopmentServerHost("C:\\Users\\...", "/")]
    [UrlToTest("http://localhost:35601/")]
    public void GetByIdTest()
    {
        string someid= "..";
        SomeObj actual = MyRepository.GetById(someid);
        Assert.AreEqual(some, SomeObj.id);
    }

How do i get this to work properly?

Upvotes: 0

Views: 222

Answers (1)

marteljn
marteljn

Reputation: 6526

Putting the settings in the app.config should solve the issue you posed above however, the more correct answer is that you should be using a mocking framework to mock the nHibernate session.

The fact that you found an area that you would need to change to accomodate testing is great!!! That is one advantage of unit testing; you find coupling in your code that should be refactored.

I found another post that addresses what you are trying to do directly Mocking an NHibernate ISession with Moq. There are two answers in the post that offer to approaches which may be helpful.

I hope this helps. I havent used nHibernate so I can't speak authoritatively about it or that the link above will provide you with an answer, but each answer has ten upvotes so it looks like it was a solid post!

Upvotes: 1

Related Questions