Bullying a datacontext for an object that has a dependency

I am writing some unit tests in my project, and I have a datacontext dependent on a controller containing methods that I would like to test.

I use Ninject for dependency injection and Moq to create my datacontext layout. My DI uses an interface IDataContextthat uses my dbml and is used through the injection process.

In my unit test, I create my datacontext layout as follows:

var mock = new Mock<IDataContext>();
var myController = new MyController(mock.Object);

This throws an exception Object reference not set to an instance of an object.in the second line when executing the datacontexts constructor.

I clearly lack a fundamental element in setting this up, however most of the Moq examples I've seen include some kind of test against a mocking object using Setup().

Am I going to do it right? Should I create a layout for my interface IDataContextor something else?

+3
source share
1 answer

haha, the answer came when I read the emad blog while unit testing in ASP.Net MVC.

I assume that you did not add a connection string in the app.config of your test project? :)

And that is the database dependency path because you are still not mocking the end of the database. Therefore, if you want to do this, you need google for some codes, there are several ways to do this.

, , , .

+2

Source: https://habr.com/ru/post/1748383/


All Articles