English 中文(简体)
单位:测试储存库
原标题:Unit Testing Repositories

我在我的项目中遵循了保存模式。 我的保存机构与“老板”进行了交谈,我正在利用实体框架作为管理事务办公室。

目前,在我的单位测试中,我直接针对常识层,因此,任何测试都直接针对非行。 因此,如果我测试插入,它实际上将数据输入数据库。

我想知道,我如何检验,而不必担心数据库是否得到修改?

是否有办法进行网上数据库测试?

增 编

我建立了与我“Data”案实际相类似的数据库图象,然后我尝试实施这一数据库。

    [TestInitialize]
    public void TestInitialize()
    {

        //TODO:  in TestCleanup, do a rollback to revert any changes performed during the test.
        Database.SetInitializer<LogViewerDbContextFake>(new LogViewerInitializer());
    }

    [TestMethod]
    public void Get_Should_Return_List_Of_Applications()
    {
        // Arrange
        LogViewerDbContext testDbContext = new LogViewerDbContextFake();
        ApplicationRepository sut = new ApplicationRepositoryImpl(testDbContext);

        // Act
        List<string> failure = sut.Get();

        // Assert
        Assert.AreEqual(4, failure.Count);
    }

 internal class LogViewerInitializer : DropCreateDatabaseIfModelChanges<LogViewerDbContextFake>
{
    protected override void Seed(LogViewerDbContextFake context)
    {
        List<LogEntry> logEntries = new List<LogEntry>()
        {
            new LogEntry{
                Id = 1,
                Application = "Application 1"
            },
            new LogEntry{
                Id = 2,
                Application = "Application 2"
            },
            new LogEntry{
                Id = 3,
                Application = "Application 3"
            },
            new LogEntry{
                Id = 4,
                Application = "Application 4"
            },
            new LogEntry{
                Id = 5,
                Application = "Application 2"
            },
            new LogEntry{
                Id = 6,
                Application = "Application 2"
            }
        };

        logEntries.ForEach(s => context.LogEntries.Add(s));
        context.SaveChanges();
    }
}

即使测试已经进行,LogerInitializer也没有在应用数据库中添加任何记录。 因此,我想知道底是什么逻辑,究竟是怎样做?

问题回答

Use a TransactionScope block to avoid committing data to the database.

using (TransactionScope scope = new TransactionScope())
{
     // test goes here

     db.SaveChanges();

    // assertions    
}

你们是否尝试利用诸如Rhinomocks或MOQ等模拟框架来改变持久性层?

Alternately you can make the argument that you are actually doing end to end testing and remove the data from the database after the test is run. You do this by running a post and pre sql scripts before and after running your test using attributes. If you are using an AOP framework such as PostSharp, this makes it very easy. If not you can still do it using contextbound objects as I have explained here

我建立了自己的存放框架,以便利用 n和EF做到这一点。 如果你感兴趣的话,请看一下,我也有一个例子,说明如何利用我的存放框架进行简单的单位测试。

rel=“nofollow”http://blog.staticave.co.nz/201110/staticave-repostern-nuget.html





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签