为Web api编写Rhino Mock测试用例

时间:2016-05-25 06:39:00

标签: c# asp.net unit-testing asp.net-mvc-3 rhino-mocks

HI已在下面创建了一个WEBAPI。我正在尝试为下面的API编写犀牛模拟我已经尝试了很多,但每次我失败

WEBAPI客户端点击web api

    [HttpPost]
    public ActionResult TestAPI(string test)
    {

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost:42327/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


            HttpResponseMessage response = client.GetAsync("api/Home/test/test").Result;
            if (response.IsSuccessStatusCode)
            {
                var result = response.Content.ReadAsStringAsync().Result;
                ViewBag.Valid = result;
                return View();
            }
            else
            {
                var result = response.Content.ReadAsStringAsync().Result;
                ViewBag.Valid = result;
                return View();
            }
        }
    }

**WEBAPI Code**


    [Route("api/{Home}/{Username}/{Password}")]
            public HttpResponseMessage Get(string Username, string Password)

            {
                HttpResponseMessage response;
                var display = Userloginvalues().Where(m => m.UserName == Username && m.Password == Password).FirstOrDefault();
                if (display != null)
                {
                    response = Request.CreateResponse(HttpStatusCode.OK);
                    response.Content = new StringContent("xxxx.", Encoding.Unicode);
                }
                else
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound);
                    response.Content = new StringContent("xxxx", Encoding.Unicode);
                }
                return response;

            }
            public static List<LoginModel> Userloginvalues()
            {
                List<LoginModel> objModel = new List<LoginModel>();
                objModel.Add(new LoginModel { UserName = "test", Password = "test" });
                return objModel;
            }

RHIN MOCKS我在下面试过,但不知道如何继续

            MockRepository mocks = new MockRepository();

            HttpResponseMessage _respone;
            _respone = login.Get("test", "test");

0 个答案:

没有答案