gpt4 book ai didi

jquery - JsonFilter 单元测试有效,但实际场景无效 - 内容类型出现错误 - 发生了什么事?

转载 作者:行者123 更新时间:2023-12-01 08:27:55 25 4
gpt4 key购买 nike

我使用了一些 JsonFilter ActionResultAttribute,它使用 newtonsoft.Json json 转换器将请求中的 json 转换为真实对象。这似乎在一些基本参数上工作得很好,但不适用于更复杂的参数,所以我为它写了一个 UT(是的,我应该 TDD,但原型(prototype)总是第一位的......)。

属性类方法:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))
{
string inputContent;
using (var sr = new StreamReader(filterContext.HttpContext.Request.InputStream))
{
inputContent = sr.ReadToEnd();
}
var result = JavaScriptConvert.DeserializeObject(inputContent, JsonDataType);
filterContext.ActionParameters[Param] = result;
}
}

现在进行单元测试(使用 Rhino):

ActionExecutingContext aec = MockRepository.GenerateMock<ActionExecutingContext>();
HttpContextBase c = MockRepository.GenerateMock<HttpContextBase>();
HttpRequestBase r = MockRepository.GenerateMock<HttpRequestBase>();
aec.Stub(x => x.HttpContext).Return(c);
aec.Stub(x => x.ActionParameters).Return(new Dictionary<String, Object>());
c.Stub(x => x.Request).Return(r);
r.Stub(x => x.ContentType).Return("application/json");
r.Stub(x => x.InputStream).Return(new MemoryStream(new UTF8Encoding().GetBytes(lookupParams)));

JsonFilter f = new JsonFilter();
f.JsonDataType = typeof(LookupParameters);
f.Param = "TestParam";
f.OnActionExecuting(aec);

是的,它没有断言任何内容,但请放心,json 已按预期转换。现在是 json 字符串:

String lookupParams = "{\"FormID\":0,\"IDFieldID\":\"\",\"NameFieldID\":\"\",\"ReturnType\":\"html\",\"SearchText\":\"\",\"AdditionalParameters\":{\"CodeLevel\":1,\"Level1ID\":\"#AccountCodes_935ab17d-74f4-4f79-990f-07898bc98868 #Level1ID\",\"Level2ID\":\"#AccountCodes_935ab17d-74f4-4f79-990f-07898bc98868 #Level2ID\",\"Level3ID\":\"#AccountCodes_935ab17d-74f4-4f79-990f-07898bc98868 #Level3ID\"}}";

现在 - 当从网站本身和 ContentType 运行时,我已进入此代码,始终显示为“application/x-www-form-urlencoded”..即使是这种情况,对象 (LookupParameters)在 ActionResult 代码本身中看到仍然填充了正确的字段!那么在我的过滤器到达之前是否有东西对其进行过滤?在我看来,因为该对象已正确从 json 转换为真实对象,并且我的代码似乎从未被调用。这很好,但其中一个属性(AdditionalParameters)是一个字典并且始终为空。为了完整起见,这里是 ActionResult 代码:

[JsonFilter(Param = "lookupParams", JsonDataType = typeof(LookupParameters))]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Find(LookupParameters lookupParams)
{
Dictionary<String, Object> addPrms = lookupParams.AdditionalParameters;
....

你也想要 jQuery 吗?这是:

    var json =
{
FormID: formId,
IDFieldID: idField,
NameFieldID: nameField,
ReturnType: "html",
SearchText: searchText,
AdditionalParameters: adPrm // this is also a json format object
};
$.get(urlToAction,
json,
SomeFunctionIRun,
"json");

最佳答案

您将断点放置在过滤器的哪个位置?您应该将其放在第一行:

if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))

您的 Controller 操作必须进行修饰:

[ObjectFilter(Param="data", JsonDataType=typeof(JsonBag))] 

您的过滤器应该正在运行,但如果 contentType 设置为“application/json”以外的任何内容,则过滤器中的代码将不会运行。当您运行应用程序时,它是否会在断点处停止,而不是在您模拟/测试时?

关于jquery - JsonFilter 单元测试有效,但实际场景无效 - 内容类型出现错误 - 发生了什么事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2282031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com