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 6805951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:39:01+00:00 2026-05-26T19:39:01+00:00

I’m using Entity Frameworc 4.1 Code First and Moq . And I want to

  • 0

I’m using Entity Frameworc 4.1 Code First and Moq. And I want to test database initializer. Also I have the abstract BaseUnitOfWork class which inherited from DbContext (so, for testing it should be mocked).

public abstract class BaseUnitOfWork : DbContext, IUnitOfWork
{
    ...
        public IDbSet<User> Users
        {
            get
            {
                return Set<User>();
            }
        }
    ...
}

User is simple POCO with three properties: Id, Login, Password.

And here is the code of the DbInitializer:

public class BaseDbInitializer : DropCreateDatabaseAlways<BaseUnitOfWork>
{
    protected override void Seed(BaseUnitOfWork context)
    {
        base.Seed(context);

        context.Set<User>().Add(new User { Login = "admin", Password = "1" });
        context.SaveChanges();
    }
}

I’m trying to test this initializer with the next test (NUnit is used):

[TestFixture]
public class BaseDbInitializerTests
{
    private BaseUnitOfWork _baseUnitOfWork;

    [TestFixtureSetUp]
    public void Init()
    {
        Database.SetInitializer(new BaseDbInitializer());
        _baseUnitOfWork = new Mock<BaseUnitOfWork>(Consts.ConnectionStringName).Object;
        _baseUnitOfWork.Database.Initialize(true);
    }

    [TestFixtureTearDown]
    public void CleanUp()
    {
        _baseUnitOfWork.Dispose();
        Database.Delete(Consts.ConnectionStringName);
    }

    [Test]
    public void ShouldInitializeBaseDb()
    {
        var repository = new Mock<BaseRepository<User>>(_baseUnitOfWork).Object;

        var firstUserInDb = repository.FindBy(x => x.Login == "admin" && x.Password == "1").SingleOrDefault();

        Assert.That(firstUserInDb, Is.Not.Null);
        Assert.That(firstUserInDb.Login, Is.EqualTo("admin"));
        Assert.That(firstUserInDb.Password, Is.EqualTo("1"));
    }
}

Unfortunately it seems like Seed method of the BaseDbInitializer class doesn’t execute. DB is recreating, but there is no any records, and I tried to debug this test and Seed method was executed during debug session.

  • 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-05-26T19:39:02+00:00Added an answer on May 26, 2026 at 7:39 pm

    The strategy DropCreateDatabaseAlways<BaseUnitOfWork> is looking for an exact type match of BaseUnitOfWork, not a derived type, and not Mock<BaseUnitOfWork>. If you need it, you’ll have to implement a copy of the strategy for the mocked type.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.