This is a migrated thread and some comments may be shown as answers.

Testing Kendo.UI datasource

1 Answer 336 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Dave Hayward
Top achievements
Rank 1
Dave Hayward asked on 26 Sep 2017, 11:50 AM

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


 

 

 

1 Answer, 1 is accepted

Sort by
0
Mihail
Telerik team
answered on 27 Sep 2017, 11:58 AM
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
Tags
General Discussions
Asked by
Dave Hayward
Top achievements
Rank 1
Answers by
Mihail
Telerik team
Share this question
or