测试积点老人 发表于 2018-12-20 15:10:02

Web Api 的单元测试

public void TestNewGreetingAdd(){//准备
var greetingName = "testName";var greetingMessage = "Hello test";var fakeRequest = new HttpRequestMessage(HttpMethod.Post,"http://localhost:XXXX/api/greeting");var greeting = new Greeting { Name = greetingName , Message = greetingMessage };var service = new GreetingController();service.Request = fakeRequest;// 操作
var response = service.PostGreeting(greeting);//这是一个方法,方法提交谓词POST,需要参数greeting。//断言
Assert.NotNull(response);Assert.Equal(HttpStatusCode.Created,response.StatusCode);Assert.Equal(new Uri("Http://localhost:XXXX/api/greeting/newgreeting"),response.Headers.Location);
}

Miss_love 发表于 2020-12-29 09:07:55

支持分享
页: [1]
查看完整版本: Web Api 的单元测试