LoginSignup
0
0

More than 1 year has passed since last update.

Chaining Assertionでのテストコード

Last updated at Posted at 2021-10-11

初めてChaining Assertionを学びましたので、備忘録として残します。

Chaining Assertionとは何か

C#の単体テストのためのライブラリです。

テストコードを書きやすくする[Chaining Assertion]とサーバーに接続しなくてもテストできる[Moq]があります。

インストール

インストールは[NuGetパッケージの管理]から行います。

メニューのプロジェクトから開きます。

toolInstall.PNG

それぞれインストールします。

ViewModelBase.csの追加

クラスを追加して、以下のように記述します。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.ComponentModel;

namespace DDD.WinForm.ViewModels
{
    public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected bool SetProperty<T>(
            ref T field, T value, [CallerMemberName]string propertyName = null)
        {
            if(Equals(field, value))
            {
                return false;
            }

            field = value;
            var h = this.PropertyChanged;
            if (h != null)
            {
                h(this, new PropertyChangedEventArgs(propertyName));
            }

            return true;
        }
    }
}

このコードを配置することによりテストするファイル(View)とテストコード(ViewModel)がデータバインドしてテスト出来るようになります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0