Is it possible to write unit test against the VirtualQueryableCollectionView? I tried to write a unit test to verify the VirtualItemCount, but it seems that the collection is tightly coupled to the UI components and therefore requires tests to run on a STA thread. Is this correct?
7 Answers, 1 is accepted
Generally VirtualQueryableCollectionView is not coupled with any UI component and our own tests are plain unit tests. For example:
[TestMethod]
public void IListIndexer_ShouldCallItemsLoading()
{
virtualQueryableCollectionViewWithSource.VirtualItemCount = 10;
virtualQueryableCollectionViewWithSource.LoadSize = 2;
bool ItemsLoadingRaised = false;
virtualQueryableCollectionViewWithSource.ItemsLoading += (s, e) =>
{
ItemsLoadingRaised = true;
};
var firstItem = ((IList)virtualQueryableCollectionViewWithSource)[0];
Assert.AreEqual(true, ItemsLoadingRaised);
}
Vlad
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
System.InvalidOperationException : The calling thread must be STA, because many UI components require this.
at System.Windows.Input.InputManager..ctor()
at System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
at System.Windows.Input.KeyboardNavigation..ctor()
at System.Windows.FrameworkElement.FrameworkServices..ctor()
at System.Windows.FrameworkElement.EnsureFrameworkServices()
at System.Windows.FrameworkElement..ctor()
at System.Windows.Controls.Control..ctor()
at Telerik.Windows.Controls.Design.Designer.get_IsInDesignMode()
at Telerik.Windows.Data.VirtualQueryableCollectionView.Dispatcher.BeginInvoke(Action action)
at Telerik.Windows.Data.VirtualQueryableCollectionView.RefreshSource()
at Telerik.Windows.Data.VirtualQueryableCollectionView.set_VirtualItemCount(Int32 value)
I'm afraid that we are out of ideas what is causing this exception at your end.
Kind regards,Vlad
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
using
NUnit.Framework;
using
Telerik.Windows.Data;
namespace
TelerikTester
{
[TestFixture]
public
class
VirtualQueryableCollectionViewTester
{
[Test]
public
void
Should_load_items_when_count_has_changed()
{
var viewModel =
new
ViewModel();
bool
itemsLoaded =
false
;
viewModel.Items.ItemsLoading += (sender, args) => itemsLoaded =
true
;
viewModel.Items.VirtualItemCount = 0;
viewModel.Items.VirtualItemCount = 1;
Assert.That(itemsLoaded, Is.True);
}
}
public
class
ViewModel
{
public
VirtualQueryableCollectionView<
object
> Items {
get
;
set
; }
public
ViewModel()
{
Items =
new
VirtualQueryableCollectionView<
object
>();
}
}
}
I've tried your test however the test just failed - no exceptions. I've attached screenshot for reference. I'm not using NUnit however - maybe this is causing the difference.
Greetings,Vlad
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
If you run the tests with a testrunner that runs in MTA mode, e.g. the R# testrunner or NCrunch, you will get the exception.
ItemsLoading will be raised only if you try to access some item by index and this item is not yet loaded.
Greetings,Vlad
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.