我有一个测试方法来测试序列化json对象。以下是方法:
[Fact]
public void Serialise_File()
{
_fakeRelativeFileName = "http:\\test.com\\" + FakeFileName;
_fakeFileName = "Leaderboard.json"
_fakeJson = "[{\"Name\": \"Paddy\"}, {\"Name\": \"Dave\"}]"
_fakeModel = new List<MockModel>
{
new MockModel()
{
Name = "Paddy"
},
new MockModel()
{
Name = "Dave"
}
};
_httpContext.Server.MapPath(_fakeFileName).Returns(_fakeRelativeFileName);
//When
var result = _jsonService.SerializeObject(_fakeFileName, _fakeModel);
//Then
_file.Received(1).WriteAllText(_fakeRelativeFileName, _fakeJson);
}
我收到错误说:
NSubstitute.Exceptions.ReceivedCallsException
Expected to receive exactly 1 call matching:
WriteAllText("http:\test.com\Leaderboard.json", "[{"Name": "Paddy"}, {"Name": "Dave"}]")
Actually received no matching calls.
Received 1 non-matching call (non-matching arguments indicated with '*' characters):
WriteAllText("http:\test.com\Leaderboard.json", *"[
{
"Name": "Paddy"
},
{
"Name": "Dave"
}
]"*)
at NSubstitute.Core.ReceivedCallsExceptionThrower.Throw(ICallSpecification callSpecification, IEnumerable`1 matchingCalls, IEnumerable`1 nonMatchingCalls, Quantity requiredQuantity)
at NSubstitute.Routing.Handlers.CheckReceivedCallsHandler.Handle(ICall call)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source, Func`2 predicate)
at NSubstitute.Routing.Route.Handle(ICall call)
at NSubstitute.Proxies.CastleDynamicProxy.CastleForwardingInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Metro.Leaderboard.Tests.Data.JsonServiceTest.Serialise_File() in JsonServiceTest.cs: line 67
我不知道为什么它没有通过测试,json对于实际和收到的电话都是一样的,所以,我做错了什么?感谢
答案 0 :(得分:1)
问题是我正在格式化下面显示的json文件:
var listContent = JsonConvert.SerializeObject(list, Formatting.Indented);
我所做的就是删除格式参数Formatting.Indented
并通过测试!