Testing Kendo.UI datasource

2 posts, 0 answers
  1. Dave Hayward
    Dave Hayward avatar
    43 posts
    Member since:
    Jul 2012

    Posted 26 Sep 2017 Link to this post

    I'm not sure if this is a JustMock question, but it may be. I'm trying to test a Kendo datasource 'read' method which is implemented in my MVC controller. The datasource is actually part of a grid definition, but I have others that are in auto-complete controls.

    The following is a fragment of my grid definition in the view.

    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(10)
        .Events(events => events.Error("error_handler").RequestEnd("onGridRequestEnd"))
        .Model(model =>
        {
            model.Id(p => p.Id);
            model.Field(p => p.Id).Editable(false);
            model.Field(p => p.PostedStr).Editable(false);
            model.Field(p => p.UpdatedStr).Editable(false);
        })
        .Read(read => read.Action("_GetBulletins", "Bulletins").Type(HttpVerbs.Get))
        .Create(create => create.Action("_CreateBulletin", "Bulletins").Type(HttpVerbs.Post).Data("sendAntiForgery"))
        .Update(update => update.Action("_UpdateBulletin", "Bulletins").Type(HttpVerbs.Post).Data("sendAntiForgery"))
        .Destroy(update => update.Action("_DeleteBulletin", "Bulletins").Type(HttpVerbs.Post).Data("sendAntiForgery"))
    )

     

    My controller methods is:

    [AcceptVerbs(HttpVerbs.Get)]
    [AjaxOnly]
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ActionResult _GetBulletins(DataSourceRequest request)
    {
        var model = (BulletinsViewModel)ViewModels.GetModel(HttpContext, Constants.Session.Model.BulletinsViewModelId);
        var enumerableModel = model.Bulletins.AsEnumerable();
        return Json(enumerableModel.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
    }

     

    and a fragment of my test is:

    var request = new DataSourceRequest()
    {
        Aggregates = new List<AggregateDescriptor>(),
        Filters = new List<IFilterDescriptor>(),
        Groups = new List<GroupDescriptor>(),
        Sorts = new List<SortDescriptor>(),
        Page = 1,
        PageSize = 10
    };
     
    // Act
    var result = controller._GetBulletins(request) as JsonResult;
    var model = result.Data as BulletinsViewModel;

     

    When I run my test, the controller throws and exception:

     

     

    System.ArgumentNullException: 'Value cannot be null.'  Message:"Value cannot be null.\r\nParameter name: source"

    Obviously I'm not setting up the 'request' properly, althought I don't exactly know what's wrong.

    Rather than spin my wheels trying to figure that out, I wonder if you can advise me on the recomended approach to testing datasources.

    TIA

    Dave


     

     

     

  2. Mihail
    Admin
    Mihail avatar
    233 posts

    Posted 27 Sep 2017 Link to this post

    Hello Dave,

    It is not clear from your post which value shouldn't be null. I am guessing that this is "model.Bulletins".
    If this is the case the following example will show how you can mock an IEnumerable property:
    IMyClass mockMyClass = Mock.Create<IMyClass>(Behavior.Strict);
    mockMyClass.Arrange((item) => item.Collection).Returns(Enumerable.Repeat(1, 1));
    Accessing the mockMyClass.Collection will return IEnumerable<int> where the Collection property is of the same type.

    Here is a link to the documentation page of JustMock if you would like to check it.

    Regards,
    Mihail
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top