Using Moq with Entity Framework 4 - entity-framework-4

I'm trying to do some Moq testing against Entity Framework 4. Is there a simple way to do it, like changing the connection type on the entities? Has anyone out there done this?
Thanks

If you're not doing it already, you should be using the POCO template for the Entity Framework if you wish to set up proper fake objects to test against. I don't know of a walkthrough that specifically covers the Moq framework, but you should be able to get the generally idea of how to write tests for the Entity Framework using fake objects via this tutorial:
http://blogs.msdn.com/b/adonet/archive/2009/12/17/walkthrough-test-driven-development-with-the-entity-framework-4-0.aspx
And also this video from Julie Lerman demonstrating the POCO template, which is possibly to what PsychoCoder was referring:
http://msdn.microsoft.com/en-us/data/ff717739

Related

ASP.NET MVC w/ EF: using Unity with the Repository Pattern for IoC

I'm trying to implement some kind of IoC on an ASP.NET MVC 3 project (built with Entity Framework for data access) by using Unity. Entities are being converted to custom objects that implement a common interface. Those custom objects are used by the controller who puts them in a "view model" and forward it to the view.
I'm following some guides but I can't find out how to configure Unity so that the right object is mapped for every controller.
Can someone point me to a good guide/tutorial/how-to? Hope I have clearly explained the problem...
I have never used Unity, so forgive me if that's a dumb/noobish question.
Thanks in advance,
Daniele Salatti
Check this post it explain how to implement Repository Pattern for EntityFrameWork
http://www.codeproject.com/KB/database/ImplRepositoryPatternEF.aspx

Generate model classes from Entity Framework for MVC2

I am following Steven Sanderson's "Pro ASP.Net MVC2 Framewok", which uses Linq2SQL. He uses abstract factory approach to support unit testing and mocking, and writes model classes in a separate project to facilitate this.
I am using Linq to Entity Framework instead of Linq2SQL, and I have built an entity model in EF from an existing database. (I am new to all of this).
I don't quite understand if I'm getting this right, but I beleive I should still generate model classes defined outside of EF (a layer between EF and presentation?), even though I could just pass Linq2EF query results entities directly in as MVC's models (security? separation of concerns?).
So I'd like EF to stub the model classes for me instead of hand-coding them all from scratch. Is there an easy way to do this, or am I mis-interpreting what I need to do?
Take a look at this blog great resource on this subject.
If you are going to use ViewModels I highly recomend using AutoMapper from souceforge

using the ASP.NET MVC2 without the Entity Framework

I am learning asp.net MVC, as I have been using the sqlconnection, sqlcommands etc from my initial phases, I would like to initially start learning asp.net MVC without using the entity framework.
Can you give me a link or any idea of using the models to process data without using the entity framework.
So without entity framework you'll be using ADO.NET (See MSDN)
Those classes you mentioned SqlConnection, SqlCommand are part of the ADO.NET framework. The two Microsoft frameworks that build on this are Entity Framework and LinqToSQL.
If you don't want to us either you have to write you own models/classes, and then methods to persist those models into your database. (This is essentially what EF does) You won't get any LINQ or designers etc.
Also, ADO.NET does have a way to create strongly typed datasets. This might help a little.
What you are doing might help you understand whats going on under the covers, but do realize frameworks like Entity-Framework save a lot of time and effort by generating models for you.

What is the best approach when trying to use POCO with EF4?

I have read here 3 approaches towards implementing POCO with entity framework namely
Create edmx model and turn off code generation so the model will not create heavy entities for you. Then you will create your POCO classes which have to follow some restrictions.
You can use the POCO template which can be downloaded to VS 2010.
Use Code First approach where you code your POCOs and you define mapping in code. To do
this you need EF 4.0 Feature CTP from here.
I was personally going to opt for the second approach as it is quicker but what are the things that I should keep in mind since it derives from ObjectContext I guess it is in a way coupled? I know the third approach gives the most flexibility but is it worth it ? Please share your thoughts regarding this..Thanks!
That link you have provided for "POCO Template" is broken - but i assume you mean this:
http://visualstudiogallery.msdn.microsoft.com/23df0450-5677-4926-96cc-173d02752313
This template can (and should) be used in conjuction with Option 1 you have stated.
That's what i use in my current application:
1 - Create EDMX Model
2 - Turn off code gen
3 - Use POCO Generator to generate POCO classes
The POCO's do not derive from anything - they are pure POCO's.
I found these two links to be invaluable when setting up my model/poco classes. Poco generation and Poco options. As mentioned above it is a good idea (certainly if using the repository pattern) to turn off code generation in the edmx and create both a derived ObjectContext class and your Poco classes via the two T4 templates provided by Microsoft.
If you use the T4 templates without making any changes to them then you will get a set of Poco classes with the "Change Tracking Proxies with Fixup" option enabled in the generated code.

Do you know some tools can help to create fake data which was used in unit test of asp.net mvc project

I was using aps.net mvc unit test, and want to create fake data instead of using real database connection, but the Linq query code and real database was huge and complicated, create fake data in unit test which can mimic real database was a huge job! Who knows some tools can help me to create fake data and mimic real database? Thanks!
Check the Isolator, it has the ability to mock almost everything including asp.net MVC, I think it will fit your needs.
You can examine the ASP.NET MVC mocking here.
Check out this Object Hydrator on CodePlex.

Resources