51Testing软件测试论坛

标题: Web Api 的单元测试 [打印本页]

作者: 测试积点老人    时间: 2018-12-20 15:10
标题: Web Api 的单元测试

[Fact]

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
支持分享




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2