My favorites | Sign in
Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Caliburn.Testability;
using ChinookMediaManager.ViewModels;
using NUnit.Framework;

namespace ChinookMediaManager.View.Test
{
public class DataBindingValidator
{
private static BindingValidator ValidatorFor(Type guiElement, Type presenterType)
{
var boundType = new BoundType(presenterType);
object instance = Activator.CreateInstance(guiElement);
return new BindingValidator(Bound.DependencyObject((DependencyObject)instance, boundType));
}

/// <summary>
/// Validate the bindings of a keyvalue pair
/// where the key is the View type and the value is the ViewModel type.
/// </summary>
/// <param name="viewViewModelDictionary">IDictionary of View type / ViewModel type</param>
/// <returns>Enumerable of validations results</returns>
public IEnumerable<ValidationResult> Validate(IDictionary<Type,Type> viewViewModelDictionary)
{
foreach (var viewViewModel in viewViewModelDictionary)
{
BindingValidator validator = ValidatorFor(viewViewModel.Key, viewViewModel.Value);
ValidationResult validatorResult = validator.Validate();
yield return validatorResult;
}
}
}

[TestFixture]
public class TestDataBindings
{
private static Type GetViewForViewModel(Type viewModelType)
{
string viewName = viewModelType.Name.Replace("ViewModel", "View");
string viewFullName = string.Format("ChinookMediaManager.GUI.Views.{0}, ChinookMediaManager.GUI", viewName);
Type viewType = Type.GetType(viewFullName, true);
return viewType;
}

[Test]
public void AllDatabindingsAreOkay()
{
bool fail = false;
var databindingValidator = new DataBindingValidator();

Type examplePresenterType = typeof(AlbumManagerViewModel);

var dictionary = examplePresenterType.Assembly.GetTypes()
.Where(type => type.Namespace.EndsWith("ViewModels"))
.ToDictionary(vmType => GetViewForViewModel(vmType), vmType => vmType);



foreach (var validationResult in databindingValidator.Validate(dictionary))
{
if(validationResult.HasErrors)
{
Console.WriteLine(validationResult.ErrorSummary);
fail = true;
}
}

fail.Should().Be.False();
}
}
}
Show details Hide details

Change log

r823 by jfromaniello on Nov 15 (6 days ago)   Diff
(minor)
Go to: 
Sign in to write a code review

Older revisions

r816 by jfromaniello on Nov 07, 2009   Diff
Removed interfaces for ViewModels.
r753 by jfromaniello on Sep 03, 2009   Diff
Added databinding test for views.
All revisions of this file

File info

Size: 2332 bytes, 74 lines
Hosted by Google Code