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

How to unit test viewmodel for correct loading of VirtualQueryableCollectionView

3 Answers 74 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Remco
Top achievements
Rank 1
Remco asked on 07 Feb 2012, 01:02 PM
Hello,

I am trying to write a unit test for my viewmodel correctly loading data into a VirtualQueryableCollectionView. The viewmodel handles the ItemsLoading event of the VirtualQueryableCollectionView to load data into the VirtualQueryableCollectionView. In my unit test I basically need to to the same as a RadGridView would do to trigger the ItemsLoading event. But I don't know which properties to set or which methods to invoke on the VirtualQueryableCollectionView to make it trigger the ItemsLoading event. Note that I am not trying to unit test the VirtualQueryableCollectionView for correctly triggering the ItemsLoading event. Telerik will have done that. I am trying to unit test my viewmodel for correctly loading data into the VirtualQueryableCollectionView. For that I need to trigger the ItemsLoading event. But I cannot get it to trigger. Setting a breakpoint in my viewmodel when the VirtualQueryableCollectionView is databound to the RadGridView indicates it is the GetItemAt method that triggers the ItemsLoading event, but me calling that method from my unit test does not trigger the ItemsLoading event. How can I make this work?

This is what my unit test looks like:
[TestMethod]
public void GivenLogbookWhenViewingEntriesThenShouldLoadEntries()
{
    var expectedEntry = new EntryPresentationModel { Id = 1, Timestamp = DateTime.UtcNow };
    var expectedEntries = new List<EntryPresentationModel> { expectedEntry };
    var expectedEntriesResult = new ServiceLoadResult<EntryPresentationModel>(expectedEntries);
 
    var contextMock = new Mock<ILogbookViewerContext>();
    contextMock.Setup(c => c.LoadEntriesByLogbookNameAsync(It.IsAny<string>(), It.IsAny<QueryBuilder<EntryPresentationModel>>(), It.IsAny<object>())).Returns(TaskEx.FromResult(expectedEntriesResult));
 
    var appServiceMock = new Mock<IAppService>();
    appServiceMock.Setup(a => a.LogbookName).Returns("LogbookName");
    appServiceMock.Setup(a => a.LoadSize).Returns(30);
    appServiceMock.Setup(a => a.RefreshInterval).Returns(TimeSpan.Zero);
 
    var exceptionManagerMock = new Mock<ExceptionManager>();
 
    EntriesViewModel.ContinuationOptions = TaskContinuationOptions.ExecuteSynchronously;
 
    var target = new EntriesViewModel(contextMock.Object, appServiceMock.Object, exceptionManagerMock.Object);
    target.Entries.SortDescriptors.Add(new SortDescriptor { Member = "Timestamp", SortDirection = ListSortDirection.Descending });
 
    var actualEntity = target.Entries.GetItemAt(0);
 
    Assert.AreSame(expectedEntry, actualEntity);
}
The EntriesViewModel.Entries property is the VirtualQueryableCollectionView.
I hope that gives enough information about what I am trying to achieve.

cheers

Remco

3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 07 Feb 2012, 01:11 PM
Hello,

 Can you post more info how you've set up VirtualQueryableCollectionView in your case?

Greetings,
Vlad
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Remco
Top achievements
Rank 1
answered on 07 Feb 2012, 01:25 PM
I was hoping I could attach the code as a zip file, but zip attachment is not allowed. So here is the EntriesViewModel:

<deleted source code>

And here is the ILogbookViewerContext interface:

<deleted source code>
0
Vlad
Telerik team
answered on 07 Feb 2012, 01:34 PM
Hi,

 Unfortunately we are not sure why the event is not called in your case. Please open support ticket and send us small project demonstrating the problem. We will check it locally and we will provide you more info. 

All the best,
Vlad
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
GridView
Asked by
Remco
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Remco
Top achievements
Rank 1
Share this question
or