任何人都可以提供帮助,我以前一直在使用测试版的asp.net web api(在asp.net mvc 4中打包)开发,一切正常。我也有一些测试基本上创建了一个控制器,并在控制器上调用Post或Get。
现在这些给我的错误说
语言不支持发布
或
语言不支持获取
如果我删除了测试项目,那么一切正常,我可以构建我的web api项目并且它可以工作。
但我的测试经常出错。我甚至删除了我的测试项目并从头开始重新创建它仍然是相同的。
我正在做的是创建一个控制器实例并调用Get / Post这些是控制器上的实际方法。
任何帮助真的很感激
由于
答案 0 :(得分:0)
他是使用NUnit和Moq测试的MVC 4的默认WebAPI项目的Web API控制器的示例单元测试。
//ValuesController.cs in GetPostExample project
using System.Collections.Generic;
using System.Web.Http;
namespace GetPostExample.Controllers
{
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post(string value)
{
}
// PUT api/values/5
public void Put(int id, string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
}
//AGetPostValuesControllerShould.cs in GetPostExampleTest project
using System.Web.Http.Controllers;
using GetPostExample.Controllers;
using Moq;
using NUnit.Framework;
namespace GetPostExampleTest
{
[TestFixture]
public class AGetPostValuesControllerShould
{
private Mock<HttpControllerContext> _controllerContext;
[SetUp]
public void Init()
{
_controllerContext = new Mock<HttpControllerContext>();
_controllerContext.SetupAllProperties();
}
[Test]
public void WhenAGetRequestIsCalledToValuesontrollerItShouldReturnAnArrayOfValues()
{
var expected = new[] {"value1", "value2"};
var valuesController = new ValuesController {ControllerContext = _controllerContext.Object};
var result = valuesController.Get();
Assert.That(result, Is.EqualTo(expected));
}
}
}