单元测试服务层

时间:2014-10-24 17:35:31

标签: c# unit-testing mstest

我目前正致力于为连接到数据库并进行更改的服务层编写单元(ish)测试。我正在使用MSTest,Autofac和NHibernate。我想确保我正确地这样做。

我想做的是:

1)设置存储一些已知静态数据的数据库。 (我想我已经想到了这一点。)

2)测试以下GetAll()方法:

public class TagService
{
    private readonly ITagRepository _tagRepository;

    public TagService(ITagRepository tagRepository)
    {
        _tagRepository = tagRepository;
    }

    public IQueryable<Tag> GetAll(bool includeDeleted = false)
    {
        if (includeDeleted)
            return _tagRepository.GetAll();

        return _tagRepository.GetAll().Where(p => p.Deleted == false);
    }
}

在我的单元测试项目中:

我实现了一个名为BaseTest

的静态类
public static class BaseTest
{
    private static bool Intialized;
    public static IContainer container;

    static BaseTest()
    {
        Intialized = SetupUnitTestDatabase();
        container = BuildKernel("unittests");
    }

    private static IContainer BuildKernel(string tenantName)
    {
        var tenantIdStrategy = new StringTenantIdentificationStrategy(tenantName);

        var builder = new ContainerBuilder();

        //Adding the tenant ID strategy into the container so controllers
        //can display output about the current tenant.
        builder.RegisterInstance(tenantIdStrategy).As<ITenantIdentificationStrategy>();
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();

        //Register Dependencies Shared Accross Tenants
        builder.RegisterModule(new GeographyModule());
        builder.RegisterModule(new ORMModule());
        builder.RegisterModule(new EmploymentModule());
        builder.RegisterModule(new AuthenticationModule());
        builder.RegisterModule(new NotificationsModule());
        builder.RegisterModule(new NationBuilderModule());
        builder.RegisterModule(new CommonModule());
        builder.RegisterModule(new ContributionsModule());
        builder.RegisterModule(new IntakeManagementModule());
        builder.RegisterModule(new StatsModule());
        builder.RegisterModule(new MatchingModule());
        builder.RegisterModule(new ExportModule());
        builder.RegisterModule(new BlueStateDigitalModule());
        builder.RegisterModule(new RevolutionMessagingModule());
        builder.RegisterModule(new TaskSchedulerModule());

        //Create multitenant container based upon application defaults
        var mtc = new MultitenantContainer(tenantIdStrategy, builder.Build());
        //register all the tenants using helper method in auth project.
        mtc = mtc.RegisterAllTenants();

        return mtc;

    }
}

对于GetAll()方法的测试,我执行以下操作:

[TestClass()]
public class TagServiceTests 
{
    [TestMethod()]
    public void GetAllTest()
    {
        //arrange
        var tagService = BaseTest.container.Resolve<TagService>();
        //act
        var result = tagService.GetAll();
        //asert
        Assert.IsTrue(result.Count() == 10);
    }
 }

此测试通过,但我不确定以这种方式设置测试是否会导致问题。重要的是我实际测试实际的数据库事务,因为我最常见的错误不是应用程序的逻辑,而是NHibernate对查询的阻塞,我需要确保在生产中不会发生这种情况。

0 个答案:

没有答案