Skip to content

Instantly share code, notes, and snippets.

@yuta-kaseda
Last active September 19, 2017 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuta-kaseda/a0458df78ec0c6b7e635486acd660583 to your computer and use it in GitHub Desktop.
Save yuta-kaseda/a0458df78ec0c6b7e635486acd660583 to your computer and use it in GitHub Desktop.
//モック用のinterfaceが必要
public interface IMockInterface
{
int Hoge { get; }
void HogeMethod();
void FugaMethod(int a);
}
public static class MockFactory
{
public static IMockInterface CreateMock()
{
//Mockクラスを作成する
var mock = Substitute.For<IMockInterface>();
//Mockクラスのフィールドやメソッドの動作を定義していく
//Hogeの参照は1を返すようにする
mock.Hoge.Return(1);
//HogeMethodの動作を定義する
mock
.When(m => m.HogeMethod())
.Do(() => { 動作内容 });
//引数ありの場合は引数を定義する
mock
.When(m => m.FugaMethod(Arg.Any<int>()))
.Do((arg) => { 動作内容 });
return mock;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment