bovender framework
C# framework that implements MVVM and more
ViewModelCollectionTest.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Collections.ObjectModel;
6 using NUnit.Framework;
7 
8 namespace Bovender.UnitTests.Mvvm
9 {
10  [TestFixture]
12  {
13  ObservableCollection<TestModel> mc;
15 
16  [SetUp]
17  public void Setup()
18  {
19  mc = new ObservableCollection<TestModel>();
20  vmc = new ViewModelCollectionForTesting(mc);
21  }
22 
23  [Test]
24  public void AddItemToModelCollection()
25  {
26  string testValue = "hello world";
27  mc.Add(new TestModel(testValue));
28  TestViewModel vm = vmc[0];
29  Assert.AreEqual(testValue, vm.Value);
30  }
31 
32  [Test]
33  public void RemoveItemFromModelCollection()
34  {
35  TestModel m = new TestModel();
36  mc.Add(m);
37  Assert.AreEqual(1, vmc.Count);
38  mc.Remove(m);
39  Assert.AreEqual(0, vmc.Count);
40  }
41 
42  [Test]
43  public void AddItemToViewModelCollection()
44  {
45  string testValue = "hello world";
46  TestModel m = new TestModel(testValue);
47  TestViewModel vm = new TestViewModel(m);
48  vmc.Add(vm);
49  Assert.AreEqual(1, mc.Count);
50  TestModel testm = mc[0];
51  Assert.AreEqual(m.Value, testm.Value);
52  }
53 
54  [Test]
55  public void RemoveItemFromViewModelCollection()
56  {
57  TestViewModel vm = new TestViewModel(new TestModel());
58  vmc.Add(vm);
59  Assert.AreEqual(1, mc.Count);
60  vmc.Remove(vm);
62  Assert.AreEqual(0, mc.Count);
63  }
64 
65  [Test]
66  public void SelectViewModels()
67  {
68  int n = 10;
69  int s = 3;
70  for (int i = 0; i < n; i++)
71  {
72  vmc.Add(new TestViewModel(new TestModel()));
73  }
74  // Select 's' number of view model objects. There's probably a more elegant way to do this.
75  vmc[1].IsSelected = true;
76  Assert.True(vmc[1].Equals(vmc.LastSelected),
77  "View model at index 1 should be the LastSelected view model but isn't.");
78  vmc[4].IsSelected = true;
79  Assert.True(vmc[4].Equals(vmc.LastSelected),
80  "View model at index 4 should be the LastSelected view model but isn't.");
81  vmc[5].IsSelected = true;
82  Assert.True(vmc[5].Equals(vmc.LastSelected),
83  "View model at index 5 should be the LastSelected view model but isn't.");
84  Assert.AreEqual(s, vmc.CountSelected, "Incorrect number of selected view models.");
85  vmc.RemoveSelected();
86  Assert.AreEqual(0, vmc.CountSelected,
87  "After deleting selected view models, CountSelected should be 0.");
88  Assert.AreEqual(n - s, vmc.Count,
89  String.Format("There should be only {0} *view model* objects left.", n - s));
90  Assert.AreEqual(n - s, mc.Count,
91  String.Format("There should be only {0} *model* objects left.", n - s));
92  }
93  }
94 }
void RemoveSelected()
Removes all selected view models from the collection.