首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IHttpActionResult与集成测试Web API v2在MS测试中的应用

IHttpActionResult与集成测试Web API v2在MS测试中的应用
EN

Stack Overflow用户
提问于 2013-12-05 00:02:32
回答 1查看 12.3K关注 0票数 8

VS2013根据我的EF上下文自动为我生成了一个web api v2控制器。我正在尝试对控制器的put部分进行单元测试。无论我做什么,我都不能让我的断言检查StatusCodeResult返回。自动生成的代码如下所示:

代码语言:javascript
复制
 // PUT api/Vendor/5
    public IHttpActionResult PutVendor(int id, Vendor vendor)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != vendor.Id)
        {
            return BadRequest();
        }

        db.Entry(vendor).State = EntityState.Modified;

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!VendorExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

我的集成测试如下所示:

代码语言:javascript
复制
        [TestMethod]
    public void PutVendor_UpdateVendorRecord_ReturnsTrue()
    {
        // Arrange
        //CleanUpVendors();

        var controller = new VendorController(ctx);
        const string vendorName = "Unit Test Company";

        // Add vendor to database
        ctx.Vendors.Add(new Vendor { Active = true, Name = vendorName });
        ctx.SaveChanges();

        var myVendor = (from v in ctx.Vendors
                        where v.Name == vendorName
                        select v).FirstOrDefault();

        // Get Newly Inserted ID
        Assert.IsNotNull(myVendor, "Vendor is Null");
        myVendor.Name = "New Name";

        // Act
        var httpActionResult = controller.PutVendor(myVendor.Id, myVendor);
        //var response = httpActionResult as OkNegotiatedContentResult<Vendor>;
        var response = httpActionResult as OkNegotiatedContentResult<Vendor>;

        // Assert


    }

我的测试有什么问题吗?我的断言应该是什么样子的?

此断言返回true:

代码语言:javascript
复制
Assert.IsInstanceOfType(httpActionResult, typeof(System.Web.Http.Results.StatusCodeResult));

但我不认为它实际上是在测试任何东西,除了有某种形式的回报。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-05 23:59:02

考虑到您对计划中的内存数据库更改的评论,以下是如何在您的场景中进行编写测试的一些示例:

代码语言:javascript
复制
    PeopleController people = new PeopleController();

    // mismatched person Id returns BadRequest
    Person person = new Person();
    person.Id = 11;
    person.Name = "John updated";

    IHttpActionResult result = people.PutPerson(10, person).Result;

    Assert.IsInstanceOfType(result, typeof(BadRequestResult));

    // ---------------------------------------------------

    // non-existing person
    Person person = new Person();
    person.Id = 1000;
    person.Name = "John updated";

    IHttpActionResult result = people.PutPerson(1000, person).Result;

    Assert.IsInstanceOfType(result, typeof(NotFoundResult));

    // --------------------------------------------------------

    //successful update of person information and its verification
    Person person = new Person();
    person.Id = 10;
    person.Name = "John updated";

    IHttpActionResult result = people.PutPerson(10, person).Result;

    StatusCodeResult statusCodeResult = result as StatusCodeResult;

    Assert.IsNotNull(statusCodeResult);
    Assert.AreEqual<HttpStatusCode>(HttpStatusCode.NoContent, statusCodeResult.StatusCode);

    //retrieve the person to see if the update happened successfully
    IHttpActionResult getPersonResult = people.GetPerson(10).Result;

    OkNegotiatedContentResult<Person> negotiatedResult = getPersonResult as OkNegotiatedContentResult<Person>;
    Assert.IsNotNull(negotiatedResult);
    Assert.AreEqual<string>(person.Name, negotiatedResult.Content.Name);
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20380094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档