开发者

Unit testing an MVC project with EF?

I am trying to start unit testing an MVC2 project, which uses the Entity Framewo开发者_运维百科rk. When I run my "hello world" test, it fails saying this:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

How can I pass the connection data (which were generated by the Entity Framework and are in the main Web.config) to the testing project?

Thanks


Depending on what unit testing framework you use you could try adding an app.config to your test-project with the right settings for EF. This works with xUnit.Net and I'm pretty sure most other test-frameworks also support this.

For completeness I do need to warn you that tests that touch the database aren't unit-tests but integration tests. Those are useful too but can become a hassle to maintain when your code changes. It's usually a good idea to test small pieces of code in isolation, this gets around problems like you describe because you won't need to access the database at all.


I would recommended using Dev Magic Fake to Mock the UI without need to use Entity framework or even DB, using Dev Magic Fake, you can run your MVC project and run the unit test without need for any DAL for more information http://devmagicfake.codeplex.com/

Thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜