TableControllers单元测试 [英] TableControllers Unit test

查看:57
本文介绍了TableControllers单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想为我的后端编写一个简单的tablecontroller单元测试?

So I'm trying to write a simple tablecontroller Unit test for my backend??

我还没有做到这一点,我所要做的就是为ApiControllers编写单元测试,但是有没有办法为TableControllers编写单元测试呢?

I havent been able to do so, all I've achieve is writing unit testing for ApiControllers but is there a way to write a Unit test for TableControllers?

我想做的是这样:

public class AuctionController : TableController<Auction>
{
    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);
        MobileServiceContext context = new MobileServiceContext();
        DomainManager = new EntityDomainManager<Auction>(context, Request);
    }

    // GET tables/Auction
    public IQueryable<Auction> GetAllAuction()
    {
        return Query(); 
    }

    // GET tables/Auction/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public SingleResult<Auction> GetAuction(string id)
    {
        return Lookup(id);
    }

    // PATCH tables/Auction/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task<Auction> PatchAuction(string id, Delta<Auction> patch)
    {
        return UpdateAsync(id, patch);
    }

    // POST tables/Auction
    public async Task<IHttpActionResult> PostAuction(Auction item)
    {
        Auction current = await InsertAsync(item);
        return CreatedAtRoute("Tables", new { id = current.Id }, current);
    }

    // DELETE tables/Auction/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task DeleteAuction(string id)
    {
        return DeleteAsync(id);
    }
}

我想做一个像这样的测试控制器:

and i wish to make a test controller like this one:

[TestClass]
public class AuctionControllerTests
{
    private readonly AuctionController _controller;

    public AuctionControllerTests()
    {
        _controller = new AuctionController();
    }

    [TestMethod]
    public void Fetch_all_existing_items()
    {
        Assert.Equal(2, _controller.GetAllTodoItems().ToList().Count);
    }
}

我怎样才能使它正常工作???拜托,非常感谢您的帮助.

how can I possibly be able to get this to work??? Please I would appreciate your help a lot.

推荐答案

因此,感谢您的模拟解决方案,它可以正常工作,但是我写了一个通用的更好的解决方案而不使用模拟框架,稍后我将应用模拟框架,现在坚持使用伪造品和真实的数据库进行集成测试.

So Thanks for the mocking solution, It worked but I wrote a generic better solution without using mocking framework, I'll apply mocking framework later, right now I'll stick with fakes and real dbs for integration tests.

但是首先,我编写了一个Generic TableController,以便为具有多个Context的用户应用多个EntityData和DbContext,由于接口的抽象,您也可以应用FakeContext,但是我没有将其应用于此示例.

but firstable I wrote a Generic TableController in order to apply multiple EntityData and DbContext for those who had more than one Context, also you could apply a FakeContext thanks to the abstraction of interfaces but i havent applied to this example.

首先,这是我的BaseController:

First This is my BaseController:

//This is an abstract class so we can apply inheritance to scalfolded tablecontrollers<T>.
public abstract class BaseController<TModel, TDbContext> : TableController<TModel> where TModel : class, ITableData
                                                                                       where TDbContext:DbContext, new()
{
    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);
        var context = new TDbContext();
        SetDomainManager(new EntityDomainManager<TModel>(context, Request));
    }

    public void SetDomainManager(EntityDomainManager<TModel> domainManager)
    {
        DomainManager = domainManager;
    }
}

这是我应用了basecontroller的折叠式控制器!!!

this is my scalfolded controller with my basecontroller applied!!!

public class AuctionController : BaseController<Auction, MobileServiceContext>    
{        
    public IQueryable<Auction> GetAllAuction()
    {
        return Query(); 
    }

    // GET tables/Auction/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public SingleResult<Auction> GetAuction(string id)
    {
        return Lookup(id);
    }

    // PATCH tables/Auction/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task<Auction> PatchAuction(string id, Delta<Auction> patch)
    {
         return UpdateAsync(id, patch);
    }

    // POST tables/Auction
    public async Task<IHttpActionResult> PostAuction(Auction item)
    {
        Auction current = await InsertAsync(item);
        return CreatedAtRoute("Tables", new { id = current.Id }, current);
    }

    // DELETE tables/Auction/48D68C86-6EA6-4C25-AA33-223FC9A27959
    public Task DeleteAuction(string id)
    {
         return DeleteAsync(id);
    }
}

通过我的通用应用程序,我可以应用任何DbContext,甚至可以应用FakeDbContexts来避免SqlConnection或云连接(例如Azure),这是我在本示例中使用的连接.

With my generic application I can apply any DbContext that way you could even apply FakeDbContexts in order to avoid SqlConnection or Cloud connection such as Azure which is the one I used in this example.

2018年3月14日更新

这两个库都在我的Backend项目中,现在我将向您展示我的测试项目,以便对TableController进行单元测试

All this two library are on my Backend project, now I'll show you my test project in order to Unit Test a TableController

 public abstract class ControllerTestBase<TController, TModel, TDbContext> where TController : BaseController<TModel, TDbContext>, new() 
                                                                           where TModel : class, ITableData
                                                                           where TDbContext: DbContext, new()
{
    protected readonly TController Controller;

    protected ControllerTestBase()
    {   
        Controller = new TController();
        Controller.Configuration = new HttpConfiguration();
        Controller.Request = new HttpRequestMessage();
        var context = new TDbContext();        
        Controller.SetDomainManager(new EntityDomainManager<TModel>(context, Controller.Request));
    }
}

好的,由于这个抽象类,您可以从测试库中取消初始化设置,因为每次运行测试时,它将调用通用测试构造函数,设置所有必要的要求,从而避免了ArgumentNullExceptions和InvalidOperationExceptions这样的常见问题.单元测试tablecontroller,因为将其初始化为ApiController并不是很直观.

Ok thanks to this abstract class you can supress the initialize setup from the testing library because each time you run a test it will call the generic test constructor, setting up all the necessary requierements and thus avoid ArgumentNullExceptions and InvalidOperationExceptions such common problem for unit testing tablecontroller since isnt quite intuitive to initialize as an ApiController.

最后,如果您对此进行修改,则可以运行如下测试:

Finally if you modify this then you can run a test like this:

[TestClass]
public class AuctionControllerTest : ControllerTestBase<AuctionController, Auction, MobileServiceContext>
{
    [TestMethod]
    public void Fetch_All_Existing_Items()
    {
        Assert.AreEqual(1, Controller.GetAllAuction().ToList().Count);
    }
}

由于我的通用应用程序,您现在可以将此代码用作应用于TableController的示例,并且如果您遵循接口隔离原则,则可以将FakeDbContext应用于控制器.

thanks to my generic application you can now use this code as an example to be apply to your TableControllers and also if you follow the Interface Segregation Principle you could apply FakeDbContext to your Controllers.

对于那些为我提供帮助的人,谢谢您为解决此问题打开了胸怀!!

For those who helped me thanks you opened my mind into coming with this solution!!!

这篇关于TableControllers单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆