Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8216257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:03:57+00:00 2026-06-07T12:03:57+00:00

After reading several questions/answers about unit testing Entity Framework, I have decided to forgo

  • 0

After reading several questions/answers about unit testing Entity Framework, I have decided to forgo unit testing for integration testing. I am going in with the philosophy that interacting with the EF context is a “private” action, because it does not need to be unit tested independently of my service, and can’t easily & accurately be mocked.

Note: In my example, I’m using EF5.

First, I have a service method for creating a user:

void CreateUser(string username, string password);

My test assembly has a SetUpFixture (one-time for test run) that creates of my database (EF Code First) and test data:

[SetUpFixture]
public class SetUpFixture
{
    [SetUp]
    public void SetUp()
    {
        using (var context = new MyDbContext())
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<MyDbContext>());

            // Set up a bunch of initial data and commit
        }
    }
}

Then before each test, my TestFixtureSetup method runs that creates an instance of my DB context, which is set to rollback when it’s disposed (after each test), and also creates the instance of my service:

[TestFixtureSetUp]
public virtual void TestFixtureSetUp()
{
    _context = new MyContext(rollbackOnDispose: true);
    UserService = new SignupService(_context);
}

[TestFixtureTearDown]
public virtual void TestFixtureTearDown()
{
    Context.Dispose();
}

Finally, my actual integration test to make sure that what valid data is passed in, a record with my username got created (and here’s where my problem lies):

[Test]
public void ValidDataShouldResultInNewRecordWithUsername()
{
    SignupService.CreateUser("myuser", "fakepassword");

    var q = from user in Context.Users 
            where user.Username == "myuser"
            select user;

    var actualUser = q.Single();

    Assert.AreEqual("myuser", actualUser.Username);
}

Here are my questions:

1) First of all, is this even the way to go testing services dependent on EF? I know there are several approaches, I just want to make sure there’s nothing crazy with this approach.

2) Second, how should I verify the service method (CreateUser) did what it was supposed to do, before the data is committed (which I don’t want it to commit, so that my database state remains as it was initialized prior to each test)? The query in the test above returns no data, since it wasn’t committed yet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:03:59+00:00Added an answer on June 7, 2026 at 12:03 pm

    This is close to how all my automated tests are (I don’t unit test either, only integration test against a real db). There are two main things I do differently.

    The first is I setup a transactionscope in my setup and dispose it in my tear down, without committing. This allows me to query for updated data w
    But be able to revert the database for the next test. The downside is that you probably can’t use transactionscopes in your service layer as I don’t know how well nested transactions work.

    The other thing I do is trigger a full database rebuild at startup by calling the EF initialize class (I don’t have my code in front of me ATM). This way I’m garanteed to have a fresh database from seed data at the beginning of the test run, and it also let’s me know if my entity structure is bad,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading several questions about .gitignore I have found no one that could answer
I have asked several questions about Zend and its search functions. Now after further
After reading several other questions, it looks like it's not advisable to have an
After reading several questions regarding problems with compilation (particularly C++) and noticing that in
I've been reading several threads and questions about this issue but I didn't find
I have planed to start using a framework in php development. After going through
Apologies for posting such long, non-compilable code. But despite reading several questions and answers
I puzzled with understanding ADO.NET, after reading several articles I do not have a
After reading several articles of a hierarchical data tables, I've made my way into
After reading monkeytalk faq from http://www.gorillalogic.com/testing-tools/monkeytalk/documentation/monkeytalk-faq : How does it all work? MonkeyTalk is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.