English 中文(简体)
• 如何测试使用JSON从请求中指示的主计长行动。 形式?
原标题:How to test Controller Action that uses JSON string from Request.Form?

我有一项行动,从<代码>查询获得初专干事数据。 表格[0],并输入域名。

我正在测试这一方法,但似乎不可能确定要求。 表格。

I could extract the method to another that takes the string it returns, but that would just be a one line method and the Action would still be untested.

是否有办法测试这一或另一种更可测试的方法,以从$.ajax(上查询初等元数据?

最佳回答

有可能通过将<条码>载的参数书写成该方法,将其作为一个参数。

<代码>公开 JsonResult ActionName (string paramName)

并将其列入数据:

var dataVar = getDataVar();
$.ajax({
    url:  /Controller/ActionName 
    , type:  post 
    , data: { paramName: dataVar }
    , dataType:  json 

    , success: function (returnJSON) {
    }
    , error: function (XMLHttpRequest, textStatus, errorThrown) {
    //error handle in here
    }
});
问题回答

Personally I Use MVCContrib TestHelper to unit test my controller actions. It makes things very fun and easy.

So in your case assuming the following controller (disclaimer: absolutely never write something like this in a real application, it s just an example here, in a real world application controller actions should never fetch stuff from Request.Form, they should use strongly typed action parameters and leave the default model binder do the parsing, etc...):

public class MyViewModel
{
    public string SomeProperty { get; set; }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var json = Request.Form[0];
        var model = new JavaScriptSerializer().Deserialize<MyViewModel>(json);
        return View(model);
    }
}

你们可以检验:

// arrange
var builder = new TestControllerBuilder();
var sut = new HomeController();
builder.InitializeController(sut);
builder.Form.Add("foo", "{ someProperty:  some value  }");

// act
var actual = sut.Index();

// assert
actual
    .AssertViewRendered()
    .WithViewData<MyViewModel>()
    .SomeProperty
    .ShouldEqual("some value", "");




相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签