本文整理汇总了C#中Mock.When方法的典型用法代码示例。如果您正苦于以下问题:C# Mock.When方法的具体用法?C# Mock.When怎么用?C# Mock.When使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mock
的用法示例。
在下文中一共展示了Mock.When方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ChooseAffirmativeExpetationOnVoidMethod
public void ChooseAffirmativeExpetationOnVoidMethod()
{
var mock = new Mock<IFoo>();
var when = true;
var positive = false;
var negative = false;
mock.When(() => when).Setup(x => x.Bar()).Callback(() => positive = true);
mock.When(() => !when).Setup(x => x.Bar()).Callback(() => negative = true);
mock.Object.Bar();
Assert.True(positive);
Assert.False(negative);
when = false;
positive = false;
mock.Object.Bar();
Assert.False(positive);
Assert.True(negative);
when = true;
negative = false;
mock.Object.Bar();
Assert.True(positive);
Assert.False(negative);
}
示例2: ChooseAffirmativeExpetationOnPropertySetter
public void ChooseAffirmativeExpetationOnPropertySetter()
{
var mock = new Mock<IFoo>();
var when = true;
var positive = false;
var negative = false;
mock.When(() => when).SetupSet(x => x.Value = "foo").Callback(() => positive = true);
mock.When(() => !when).SetupSet(x => x.Value = "foo").Callback(() => negative = true);
mock.Object.Value = "foo";
Assert.True(positive);
Assert.False(negative);
when = false;
positive = false;
mock.Object.Value = "foo";
Assert.False(positive);
Assert.True(negative);
when = true;
negative = false;
mock.Object.Value = "foo";
Assert.True(positive);
Assert.False(negative);
}
示例3: ChooseAffirmativeExpectationOnPropertyIndexer
public void ChooseAffirmativeExpectationOnPropertyIndexer()
{
var mock = new Mock<IFoo>();
var first = true;
mock.When(() => first).Setup(x => x[0]).Returns("bar");
mock.When(() => !first).Setup(x => x[0]).Returns("no bar");
Assert.Equal("bar", mock.Object[0]);
first = false;
Assert.Equal("no bar", mock.Object[0]);
first = true;
Assert.Equal("bar", mock.Object[0]);
}
示例4: MethodCallReturnsValue
public void MethodCallReturnsValue()
{
var mock = new Mock<IBlah>();
mock.When(o => o.GetThing()).Return(10);
var blah = mock.Object;
Assert.AreEqual(10, blah.GetThing());
}
示例5: MethodCallInvalid
public void MethodCallInvalid()
{
var mock = new Mock<IBlah>();
mock.When(o => o.DoThing(5)).CheckArguments();
var blah = mock.Object;
blah.DoThing(4);
}
示例6: IgnoreSpecificCalls
public void IgnoreSpecificCalls()
{
var mock = new Mock<IBlah>();
mock.When(o => o.GetThing()).Ignore();
var blah = mock.Object;
Assert.AreEqual(default(int), blah.GetThing());
int i = blah.Thing;
}
示例7: ChooseAffirmativeExpectationOnMethod
public void ChooseAffirmativeExpectationOnMethod()
{
var mock = new Mock<IFoo>();
var when = true;
mock.When(() => when).Setup(x => x.Foo()).Returns("bar");
mock.When(() => !when).Setup(x => x.Foo()).Returns("no bar");
Assert.Equal("bar", mock.Object.Foo());
when = false;
Assert.Equal("no bar", mock.Object.Foo());
when = true;
Assert.Equal("bar", mock.Object.Foo());
}
示例8: ExpectedExceptionWhenToIsSpecificMail
public void ExpectedExceptionWhenToIsSpecificMail()
{
Mock<IServicioCorreo> mMockMailSvc = new Mock<IServicioCorreo>();
//Se prepara el mock
mMockMailSvc.When(() => this.iMailDTO.CuentaDestino.Equals("[email protected]")).Setup(x => x.EnviarCorreo(this.iMailDTO, this.iAccountDTO)).Throws<Exception>();
IServicioCorreo mMailSvc = mMockMailSvc.Object;
mMailSvc.EnviarCorreo(this.iMailDTO, this.iAccountDTO);
}
示例9: MethodCallThrows
public void MethodCallThrows()
{
var mock = new Mock<IBlah>();
mock.When(o => o.GetThing()).Throw();
var blah = mock.Object;
int i = blah.GetThing();
}
示例10: ReadOnlyPropertyReturnsCorrectValue
public void ReadOnlyPropertyReturnsCorrectValue()
{
var mock = new Mock<IBlah>();
mock.When(o => o.ReadOnly).Return("thing");
var blah = mock.Object;
Assert.AreEqual("thing", blah.ReadOnly);
}
示例11: PropertyGetThrows
public void PropertyGetThrows()
{
var mock = new Mock<IBlah>();
mock.When(o => o.Thing).Throw();
var blah = mock.Object;
int i = blah.Thing;
}
示例12: MethodCallWithArgsThrows
public void MethodCallWithArgsThrows()
{
var mock = new Mock<IBlah>();
mock.When(o => o.DoThing(5)).WithExactArguments().Throw();
var blah = mock.Object;
blah.DoThing(5);
}