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

Unit testing VirtualQueryableCollectionView

7 Answers 94 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kjetil
Top achievements
Rank 1
Kjetil asked on 11 Jan 2013, 02:19 PM

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

Sort by
0
Vlad
Telerik team
answered on 11 Jan 2013, 02:26 PM
Hello,

 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);
        }

Regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Kjetil
Top achievements
Rank 1
answered on 16 Jan 2013, 07:26 AM
Then why do I get the following stack trace and exception when I set the VirtualItemCount;

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)

0
Vlad
Telerik team
answered on 16 Jan 2013, 07:52 AM
Hi,

 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.

0
Kjetil
Top achievements
Rank 1
answered on 16 Jan 2013, 09:46 AM
Shouldn't be hard to reproduce though. Here's a test that gives the InvalidOperationException;

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>();
        }
    }
}
0
Vlad
Telerik team
answered on 16 Jan 2013, 09:59 AM
Hello,

 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.

0
Kjetil
Top achievements
Rank 1
answered on 16 Jan 2013, 10:20 AM
I'm guessing you don't get the exception because the MSTestrunner is running in a STA Thread. However, shouldn't the test suceed in this case? Shouldn't the ItemsLoading event be triggered when VirtualItemCount is set?

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.
0
Accepted
Vlad
Telerik team
answered on 16 Jan 2013, 11:19 AM
Hello,

 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.

Tags
GridView
Asked by
Kjetil
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Kjetil
Top achievements
Rank 1
Share this question
or