我有一个返回匿名对象的json数组的动作:
public JsonResult GetItems()
{
var items = new[] { new { Id = 1, Name = "One" }, new { Id = 2, Name = "Two" } };
return Json(items);
}
如何为该方法编写测试?像这样:
[TestMethod]
public void MyController_GetItems()
{
var controller = new MyController();
var result = controller.GetItems();
dynamic items = result.Data;
Assert.AreEqual("One", items[0].Name); // ???
}
答案 0 :(得分:1)
这样的事情怎么样: JsonNetResult是一个自定义ActionResult,用JSON.net替换内部Microsoft Json序列化程序
using FakeItEasy;
using Newtonsoft.Json;
using NUnit.Framework;
using System;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
public class JsonNetResultFixture
{
private JsonNetResult _sut;
private Controller fakeController;
private HttpContextBase fakeContext;
private HttpRequestBase fakeRequest;
private HttpResponseBase fakeResponse;
private RequestContext fakeRequestContext;
[SetUp]
public void Setup()
{
fakeController = A.Fake<Controller>();
fakeContext = A.Fake<HttpContextBase>();
fakeRequest = A.Fake<HttpRequestBase>();
fakeResponse = A.Fake<HttpResponseBase>();
fakeRequestContext = new RequestContext(fakeContext, new RouteData());
fakeController.ControllerContext = new ControllerContext(fakeRequestContext, fakeController);
A.CallTo(() => fakeRequest.HttpMethod).Returns(HttpVerbs.Post.ToString());
A.CallTo(() => fakeContext.Response).Returns(fakeResponse);
A.CallTo(() => fakeContext.Request).Returns(fakeRequest);
_sut = new JsonNetResult();
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void should_throw_argumentexception_if_context_is_null()
{
_sut.ExecuteResult(null);
}
[Test]
public void should_write_serialized_class_to_output_stream()
{
//Arrange
var sb = new StringBuilder();
_sut.Data = new SerializableClass { Id = Guid.NewGuid(), Payload = "awesome payload" };
var serializedData = JsonConvert.SerializeObject(_sut.Data, Formatting.Indented);
A.CallTo(() => fakeResponse.Write(A<String>.Ignored)).Invokes((String x) => sb.Append(x));
//Act
_sut.ExecuteResult(fakeController.ControllerContext);
//Assert
Assert.That(sb.ToString(), Is.EqualTo(serializedData));
}
[Test]
public void should_write_nothing_when_data_is_nothing()
{
//Arrange
var sb = new StringBuilder();
A.CallTo(() => fakeResponse.Write(A<String>.Ignored)).Invokes((String x) => sb.Append(x)).MustHaveHappened(Repeated.Never);
//Act
_sut.ExecuteResult(fakeController.ControllerContext);
//Assert
Assert.That(sb.ToString(), Is.EqualTo(String.Empty));
}
[Test]
public void should_set_content_type_if_sent_in()
{
//Arrange
var contentType = "text/xml";
_sut.ContentType = contentType;
//Act
_sut.ExecuteResult(fakeController.ControllerContext);
//Assert
Assert.That(_sut.ContentType, Is.EqualTo(contentType));
}
[Test]
public void should_set_content_encoding_if_sent_in()
{
//Arrange
var contentEncoding = Encoding.BigEndianUnicode;
_sut.ContentEncoding = contentEncoding;
//Act
_sut.ExecuteResult(fakeController.ControllerContext);
//Assert
Assert.That(_sut.ContentEncoding, Is.EqualTo(contentEncoding));
}
}
public class SerializableClass
{
public Guid Id { get; set; }
public String Payload { get; set; }
}
答案 1 :(得分:0)
嗯,返回类型仍然是JsonResult
。如果你在文档中探讨,应该有一种方法来检查它的值。
http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult(v=vs.118).aspx
看起来你得到的只是Object
类型。所以,我认为你所做的是你能做的最好的事情。
答案 2 :(得分:0)
var items = (dynamic[])result.Data;
Assert.AreEqual("One", items[0].Name);