如何在c#中测试视图模型是否处于设计器模式

时间:2021-01-05 15:52:14

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).

我是C#和WPF的新手,所以我想从MVVM开始。我有一个小的WPF应用程序,我想测试我的视图模型是否在Designer模式下创建(检查DesignerProperties);鉴于我有一个IDataService,它从硬编码列表(设计时)或REST服务(运行时)向ViewModel提供数据。

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

有没有办法模拟或存根这个DesignerProperties对象来强制它成为一个或另一个状态?

Thanks in advance.

提前致谢。

2 个解决方案

#1


3  

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

有没有办法模拟或存根这个DesignerProperties对象来强制它成为一个或另一个状态?

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

不,这是一个静态类;除非你使用“Microsoft Fakes”或“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.

但是你可以为DesignerProperties创建一个抽象,说IDesignerProperties,它有你感兴趣的方法/属性并注入它。这样它现在只是一个界面;您可以像对待所有其他依赖项一样模拟它。

#2


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.

您可以为静态类创建一个包装器。我不熟悉DesignerProperties类,但我在下面创建了一个示例。另请参阅依赖注入/控制反转,以便于单元测试。

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)

依赖注入和模拟的接口。 (您可以通过反映静态类使用T4模板进行自动生成)

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.

我希望这能帮到您。

#1


3  

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

有没有办法模拟或存根这个DesignerProperties对象来强制它成为一个或另一个状态?

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

不,这是一个静态类;除非你使用“Microsoft Fakes”或“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.

但是你可以为DesignerProperties创建一个抽象,说IDesignerProperties,它有你感兴趣的方法/属性并注入它。这样它现在只是一个界面;您可以像对待所有其他依赖项一样模拟它。

#2


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.

您可以为静态类创建一个包装器。我不熟悉DesignerProperties类,但我在下面创建了一个示例。另请参阅依赖注入/控制反转,以便于单元测试。

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)

依赖注入和模拟的接口。 (您可以通过反映静态类使用T4模板进行自动生成)

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.

我希望这能帮到您。