4

I'm new to C# and WPF, so I wanted to start by the book with MVVM. I have a small WPF app and I'd like to test if my view model is created in Designer mode or not (checking DesignerProperties); given that I have an IDataService that provides data to the ViewModel either from a hardcoded list (Design time) or a REST service (Runtime).

Is there a way to Mock or Stub this DesignerProperties object to force it to be one or the other state?

Thanks in advance.

CoderX
  • 162
  • 1
  • 9
  • Possible duplicate of [WPF MVVM ViewModel constructor designmode](http://stackoverflow.com/questions/2498521/wpf-mvvm-viewmodel-constructor-designmode) – Khale_Kitha Feb 22 '16 at 19:30
  • Thanks Khale, but my question points towards writing a unit test (if it's possible), while your suggested post explains how to write the view model to check for the scenario I want to test (which I already implemented in my code). – CoderX Feb 22 '16 at 19:38
  • My fault - I misread your request. – Khale_Kitha Feb 22 '16 at 19:39

2 Answers2

3

Is there a way to Mock or Stub this DesignerProperties object to force it to be one or the other state?

No. It is a static class; you can't mock that easily unless you're using "Microsoft Fakes" or "Type Mock".

But you could create an abstraction for DesignerProperties say IDesignerProperties which has methods/properties of your interest and inject it. That way it's just an interface now; you can mock it as you do for all other dependencies.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • So you mean using my own 'custom-made' DesignerProperties to 'replace' the original functionality with my own? Just want to be sure of your proposal... – CoderX Feb 22 '16 at 20:14
  • Yes, create your own class which implements `IDesignerProperties` interface and add required methods. – Sriram Sakthivel Feb 23 '16 at 06:28
2

You can make a wrapper for the static class. I'm not familiar with DesignerProperties class, but i've created an example below. Also look into Dependency Injection / Inversion of Control, for ease of unit testing.

The static class

static class DesignerProperties
{
    public bool IsInDesigner { get; }

    public void DoSomething(string arg);
    // Other properties and methods
}

The interface for Dependency Injection and mocking. (You can use T4 templates for autogeneration via reflection of the static class)

interface IDesignerProperties
{
    bool IsInDesigner { get; }

    void DoSomething(string arg);
    // mimic properties and methods from the static class here
}

The actual class for runtime usage

class DesignerPropertiesWrapper : IDesignerProperties
{
    public bool IsInDesigner 
    {
        get { return DesignerProperties.IsInDesigner; } 
    }

    public void DoSomething(string arg)
    {
        DesignerProperties.DoSomething(arg);
    }

    // forward other properties and methods to the static class
}

Mocking class for Unit Testing

class DesignerpropertiesMock : IDesignerProperties
{
    public bool IsInDesigner { get; set; } //setter accessible for Mocking
}

Usage

class ViewModel 
{
    private readonly IDesignerProperties _designerProperties;

    // Inject the proper implementation
    public ViewModel(IDesignerProperties designerProperties)
    {
        _designerProperties = designerProperties;
    }
}

I hope this will help you.

Anders
  • 1,590
  • 1
  • 20
  • 37
  • Thanks, this is exactly the clarification from Sriram's answer, clearing my doubts about the implementation aspect. – CoderX Feb 22 '16 at 20:18