Upload
others
View
21
Download
0
Embed Size (px)
Citation preview
C#
MoqMoqNamespaceSendFeedbackRootnamespaceofMoq(pronounced"Mock-you"orjust"Mock"),theonlymockinglibraryfor.NETdevelopedfromscratchtotakefulladvantageof.NET3.5(i.e.Linqexpressiontrees)andC#3.0features(i.e.lambdaexpressions)thatmakeitthemostproductive,simpleandrefactoring-friendlymockinglibraryavailable.
Seetheonlinequickstartsformoreexamplesthanthoseavailableinthiscodedocumentation.
TheMock<T>classisthecoreofthelibrary,soit'sagoodplacetostart.
YoucanalsoreadblogentriesfromaroundtheworldaboutMoq.
Classes Class Description
It
Allowsthespecificationofamatchingconditionforanargumentinamethodinvocation,ratherthanaspecificargumentvalue."It"referstotheargumentbeingmatched.
MatcherAttributeMarksamethodasamatcher,whichallowscompletereplacementofthebuilt-inItclasswithyourownargumentmatchingrules.
Mock
Baseclassformocksandstatichelperclasswithmethodsthatapplytomockedobjects,suchasGet<(Of<(T>)>)(T)toretrieveaMock<(Of<(T>)>)fromanobjectinstance.
Mock<(Of<(T>)>)
CoreimplementationoftheIMock<(Of<(T>)>)interface.
MockedEvent Representsagenericeventthathasbeenmockedandcanberised.
MockedEvent<(Of<(TEventArgs>)>)
ProvidesatypedMockedEventforaspecifictypeofEventArgs.
MockException Exceptionthrownbymockswhenexpectationsarenotmet,themockisnotproperlysetup,etc.
MockFactoryUtilityfactoryclasstousetoconstructmultiplemockswhenconsistentverificationisdesiredforallofthem.
Interfaces Interface Description
IMock Basemockinterfaceexposingnon-genericmembers.
IMock<(Of<(T>)>) ProvidesamockimplementationofT.
Enumerations Enumeration Description
DefaultValue Determinesthewaydefaultvaluesaregeneratedcalculatedforloosemocks.
MockBehavior Optionstocustomizethebehaviorofthemock.
Range KindofrangetouseinafilterspecifiedthroughIsInRange<(Of<(TValue>)>)(TValue,TValue,Range).
C#
MoqDefaultValueEnumerationSeeAlsoSendFeedbackDeterminesthewaydefaultvaluesaregeneratedcalculatedforloosemocks.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicenumDefaultValue
MembersMembername Description
EmptyDefaultbehavior,whichgeneratesemptyvaluesforvaluetypes(i.e.default(int)),emptyarrayandenumerables,andnullsforallotherreferencetypes.
Mock WheneverthedefaultvaluegeneratedbyEmptyisnull,replacesthisvaluewithamock(ifthetypecanbemocked).
C#
MoqIMockInterfaceMembersSeeAlsoSendFeedbackBasemockinterfaceexposingnon-genericmembers.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceIMock
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIMockMembersIMockInterfaceMethodsPropertiesSeeAlsoSendFeedback
TheIMocktypeexposesthefollowingmembers.
Methods Name Description
CreateEventHandler Overloaded.
Verify Verifiesthatallverifiableexpectationshavebeenmet.
VerifyAll Verifiesallexpectationsregardlessofwhethertheyhavebeenflaggedasverifiable.
Properties Name Description
CallBaseWhetherthebasemembervirtualimplementationwillbecalledformockedclassesifnoexpectationismet.DefaultstotrueTruetruetrue(TrueinVisualBasic).
DefaultValue Determineshowtogeneratedefaultvaluesforloosemocksonunexpectedinvocations.Object Themockedobjectinstance.
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIMockMethodsIMockInterfaceSeeAlsoSendFeedback
TheIMocktypeexposesthefollowingmembers.
Methods Name Description
CreateEventHandler Overloaded.
Verify Verifiesthatallverifiableexpectationshavebeenmet.
VerifyAll Verifiesallexpectationsregardlessofwhethertheyhavebeenflaggedasverifiable.
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIMock..::.CreateEventHandlerMethodIMockInterfaceSeeAlsoSendFeedback
OverloadList Name Description
CreateEventHandler<(Of<(TEventArgs>)>)()()()
CreatesahandlerthatcanbeassociatedtoaneventreceivingthegivenTEventArgsandcanbeusedtoraisetheevent.
CreateEventHandler()()()CreatesahandlerthatcanbeassociatedtoaneventreceivingagenericEventArgsandcanbeusedtoraisetheevent.
SeeAlsoIMockInterfaceIMockMembersMoqNamespace
C#
MoqIMock..::.CreateEventHandler<(Of<(TEventArgs>)>)MethodIMockInterfaceExampleSeeAlsoSendFeedbackCreatesahandlerthatcanbeassociatedtoaneventreceivingthegivenTEventArgsandcanbeusedtoraisetheevent.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
MockedEvent<TEventArgs>CreateEventHandler<TEventArgs>()
whereTEventArgs:EventArgs
TypeParametersTEventArgs
TypeofEventArgsdatapassedintotheevent.
Copy
ExamplesThisexampleshowshowtoinvokeaneventwithacustomeventargumentsclassinaviewthatwillcauseitscorrespondingpresentertoreactbychangingitsstate:
C#
varmockView=newMock<IOrdersView>();
varmockedEvent=mockView.CreateEventHandler<OrderEventArgs>();
varpresenter=newOrdersPresenter(mockView.Object);
//Checkthatthepresenterhasnoselectionbydefault
Assert.Null(presenter.SelectedOrder);
//Createamockeventhandleroftheappropriatetype
varhandler=mockView.CreateEventHandler<OrderEventArgs>();
//Associateitwiththeeventwewanttoraise
mockView.Object.Cancel+=handler;
//Finallyraisetheeventwithaspecificargumentsdata
handler.Raise(newOrderEventArgs{Order=newOrder("moq",500)});
//Nowthepresenterreactedtotheevent,andwehaveaselectedorder
Assert.NotNull(presenter.SelectedOrder);
Assert.Equal("moq",presenter.SelectedOrder.ProductName);
SeeAlsoIMockInterfaceCreateEventHandlerOverloadMoqNamespace
C#
MoqIMock..::.CreateEventHandlerMethodIMockInterfaceExampleSeeAlsoSendFeedbackCreatesahandlerthatcanbeassociatedtoaneventreceivingagenericEventArgsandcanbeusedtoraisetheevent.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
MockedEvent<EventArgs>CreateEventHandler()
Copy
ExamplesThisexampleshowshowtoinvokeagenericeventinaviewthatwillcauseitscorrespondingpresentertoreactbychangingitsstate:
C#
varmockView=newMock<IOrdersView>();
varmockedEvent=mockView.CreateEventHandler();
varpresenter=newOrdersPresenter(mockView.Object);
//Checkthatthepresenterisnotinthe"Canceled"state
Assert.False(presenter.IsCanceled);
//Createamockeventhandleroftheappropriatetype
varhandler=mockView.CreateEventHandler();
//Associateitwiththeeventwewanttoraise
mockView.Object.Cancel+=handler;
//Finallyraisetheevent
handler.Raise(EventArgs.Empty);
//Nowthepresenterreactedtotheevent,andchangeditsstate
Assert.True(presenter.IsCanceled);
SeeAlsoIMockInterfaceCreateEventHandlerOverloadMoqNamespace
C#
MoqIMock..::.VerifyMethodIMockInterfaceExampleSeeAlsoSendFeedbackVerifiesthatallverifiableexpectationshavebeenmet.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
voidVerify()
Copy
ExamplesThisexamplesetsupanexpectationandmarksitasverifiable.Afterthemockisused,aVerify()()()callisissuedonthemocktoensurethemethodintheexpectationwasinvoked:
C#
varmock=newMock<IWarehouse>();
mock.Expect(x=>x.HasInventory(TALISKER,50)).Verifiable().Returns(
...
//othertestcode
...
//Willthrowifthetestcodehasdidn'tcallHasInventory.
mock.Verify();
ExceptionsException Condition
Moq..::.MockException Notallverifiableexpectationsweremet.
C#
MoqIMock..::.VerifyAllMethodIMockInterfaceExampleSeeAlsoSendFeedbackVerifiesallexpectationsregardlessofwhethertheyhavebeenflaggedasverifiable.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
voidVerifyAll()
Copy
ExamplesThisexamplesetsupanexpectationwithoutmarkingitasverifiable.Afterthemockisused,aVerifyAll()()()callisissuedonthemocktoensurethatallexpectationsaremet:
C#
varmock=newMock<IWarehouse>();
mock.Expect(x=>x.HasInventory(TALISKER,50)).Returns(true);
...
//othertestcode
...
//Willthrowifthetestcodehasdidn'tcallHasInventory,even
//thatexpectationwasnotmarkedasverifiable.
mock.VerifyAll();
ExceptionsException Condition
Moq..::.MockException Atleastoneexpectationwasnotmet.
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIMockPropertiesIMockInterfaceSeeAlsoSendFeedback
TheIMocktypeexposesthefollowingmembers.
Properties Name Description
CallBaseWhetherthebasemembervirtualimplementationwillbecalledformockedclassesifnoexpectationismet.DefaultstotrueTruetruetrue(TrueinVisualBasic).
DefaultValue Determineshowtogeneratedefaultvaluesforloosemocksonunexpectedinvocations.Object Themockedobjectinstance.
C#
MoqIMock..::.CallBasePropertyIMockInterfaceSeeAlsoSendFeedbackWhetherthebasemembervirtualimplementationwillbecalledformockedclassesifnoexpectationismet.DefaultstotrueTruetruetrue(TrueinVisualBasic).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
boolCallBase{get;set;}
C#
MoqIMock..::.DefaultValuePropertyIMockInterfaceSeeAlsoSendFeedbackDetermineshowtogeneratedefaultvaluesforloosemocksonunexpectedinvocations.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
DefaultValueDefaultValue{get;set;}
C#
MoqIMock..::.ObjectPropertyIMockInterfaceSeeAlsoSendFeedbackThemockedobjectinstance.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
ObjectObject{get;}
C#
MoqIMock<(Of<(T>)>)InterfaceMembersExampleSeeAlsoSendFeedbackProvidesamockimplementationofT.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceIMock<T>
whereT:class
TypeParametersT
Typetomock,whichcanbeaninterfaceoraclass.
RemarksOnlyabstractandvirtualmembersofclassescanbemocked.
ThebehaviorofthemockwithregardstotheexpectationsandtheactualcallsisdeterminedbytheoptionalMockBehaviorthatcanbepassedtotheMock<(Of<(T>)>)(MockBehavior)constructor.
Copy
Copy
ExamplesThefollowingexampleshowssettingexpectationswithspecificvaluesformethodinvocations:
C#
//setup-data
varorder=newOrder(TALISKER,50);
varmock=newMock<IWarehouse>();
//setup-expectations
mock.Expect(x=>x.HasInventory(TALISKER,50)).Returns(true);
//exercise
order.Fill(mock.Object);
//verify
Assert.True(order.IsFilled);
ThefollowingexampleshowshowtousetheItclasstospecifyconditionsforargumentsinsteadofspecificvalues:
C#
//setup-data
varorder=newOrder(TALISKER,50);
varmock=newMock<IWarehouse>();
//setup-expectations
//showshowtoexpectavaluewithinarange
mock.Expect(x=>x.HasInventory(
It.IsAny<string>(),
It.IsInRange(0,100,Range.Inclusive)))
.Returns(false);
//showshowtothrowforunexpectedcalls.contrastwiththe"verify"approachofothermocklibraries.
mock.Expect(x=>x.Remove(
It.IsAny<string>(),
It.IsAny<int>()))
.Throws(newInvalidOperationException());
//exercise
order.Fill(mock.Object);
//verify
Assert.False(order.IsFilled);
SeeAlsoIMock<(Of<(T>)>)MembersMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIMock<(Of<(T>)>)MembersIMock<(Of<(T>)>)InterfaceMethodsPropertiesSeeAlsoSendFeedback
TheIMock<(Of<(T>)>)typeexposesthefollowingmembers.
Methods Name Description
As<(Of<(TInterface>)>)
Addsaninterfaceimplementationtothemock,allowingexpectationstobesetforit.
Expect Overloaded.ExpectGet<(Of<(TProperty>)>)
Setsanexpectationonthemockedtypeforacalltotoapropertygetter.
ExpectSet Overloaded.Verify Overloaded.VerifyAll ImplementsVerifyAll()()().VerifyGet<(Of<(TProperty>)>)
Verifiesthatapropertywasreadonthemock.UseinconjuntionwiththedefaultLoose.
VerifySet Overloaded.
Properties Name Description
Behavior Behaviorofthemock,accordingtothevaluesetintheconstructor.
DefaultValue Specifiesthebehaviortousewhenreturningdefaultvaluesforunexpectedinvocations.Object Exposesthemockedobjectinstance.
SeeAlsoIMock<(Of<(T>)>)InterfaceMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIMock<(Of<(T>)>)MethodsIMock<(Of<(T>)>)InterfaceSeeAlsoSendFeedback
TheIMock<(Of<(T>)>)typeexposesthefollowingmembers.
Methods Name Description
As<(Of<(TInterface>)>)
Addsaninterfaceimplementationtothemock,allowingexpectationstobesetforit.
Expect Overloaded.ExpectGet<(Of<(TProperty>)>)
Setsanexpectationonthemockedtypeforacalltotoapropertygetter.
ExpectSet Overloaded.Verify Overloaded.VerifyAll ImplementsVerifyAll()()().VerifyGet<(Of<(TProperty>)>)
Verifiesthatapropertywasreadonthemock.UseinconjuntionwiththedefaultLoose.
VerifySet Overloaded.
SeeAlsoIMock<(Of<(T>)>)InterfaceMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.As<(Of<(TInterface>)>)MethodIMock<(Of<(T>)>)InterfaceExampleSeeAlsoSendFeedbackAddsaninterfaceimplementationtothemock,allowingexpectationstobesetforit.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IMock<TInterface>As<TInterface>()
whereTInterface:class
TypeParametersTInterface
Typeofinterfacetocastthemockto.
RemarksThismethodcanonlybecalledbeforethefirstuseofthemockObjectproperty,atwhichpointtheruntimetypehasalreadybeengeneratedandnomoreinterfacescanbeaddedtoit.
Also,TInterfacemustbeaninterfaceandnotaclass,whichmustbespecifiedwhencreatingthemockinstead.
Copy
ExamplesThefollowingexamplecreatesamockforthemaininterfaceandlateraddsIDisposabletoittoverifyit'scalledbytheconsumercode:
C#
varmock=newMock<IProcessor>();
mock.Expect(x=>x.Execute("ping"));
//addIDisposableinterface
vardisposable=mock.As<IDisposable>();
disposable.Expect(d=>d.Dispose()).Verifiable();
ExceptionsException Condition
InvalidOperationException ThemocktypehasalreadybeengeneratedbyaccessingtheObjectproperty.ArgumentException TheTInterfacespecifiedisnotaninterface.
SeeAlsoIMock<(Of<(T>)>)InterfaceMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIMock<(Of<(T>)>)..::.ExpectMethodIMock<(Of<(T>)>)InterfaceSeeAlsoSendFeedback
OverloadList Name Description
Expect(Expression<(Of<(Action<(Of<(T>)>)>)>))
Setsanexpectationonthemockedtypeforacalltotoavoidmethod.
Expect<(Of<(TResult>)>)(Expression<(Of<(Func<(Of<(T,TResult>)>)>)>))
Setsanexpectationonthemockedtypeforacalltotoavaluereturningmethod.
SeeAlsoIMock<(Of<(T>)>)InterfaceIMock<(Of<(T>)>)MembersMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.ExpectMethod(<(Of<(<(Of<(T>)>)>)>))IMock<(Of<(T>)>)InterfaceExampleSeeAlsoSendFeedbackSetsanexpectationonthemockedtypeforacalltotoavoidmethod.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IExpectExpect(
Expression<Action<T>>expression
)
Parameters
expressionType:Expression<(Of<(Action<(Of<(T>)>)>)>)Lambdaexpressionthatspecifiestheexpectedmethodinvocation.
RemarksIfmorethanoneexpectationissetforthesamemethodorproperty,thelatestonewinsandistheonethatwillbeexecuted.
Copy
ExamplesC#
varmock=newMock<IProcessor>();
mock.Expect(x=>x.Execute("ping"));
SeeAlsoIMock<(Of<(T>)>)InterfaceExpectOverloadMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.Expect<(Of<(TResult>)>)Method(<(Of<(<(Of<(T,TResult>)>)>)>))IMock<(Of<(T>)>)InterfaceExampleSeeAlsoSendFeedbackSetsanexpectationonthemockedtypeforacalltotoavaluereturningmethod.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IExpect<TResult>Expect<TResult>(
Expression<Func<T,TResult>>expression
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TResult>)>)>)>)Lambdaexpressionthatspecifiestheexpectedmethodinvocation.
TypeParametersTResult
Typeofthereturnvalue.Typicallyomittedasitcanbeinferredfromtheexpression.
RemarksIfmorethanoneexpectationissetforthesamemethodorproperty,thelatestonewinsandistheonethatwillbeexecuted.
Copy
ExamplesC#
mock.Expect(x=>x.HasInventory("Talisker",50)).Returns(true);
SeeAlsoIMock<(Of<(T>)>)InterfaceExpectOverloadMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.ExpectGet<(Of<(TProperty>)>)MethodIMock<(Of<(T>)>)InterfaceExampleSeeAlsoSendFeedbackSetsanexpectationonthemockedtypeforacalltotoapropertygetter.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IExpectGetter<TProperty>ExpectGet<TProperty>(
Expression<Func<T,TProperty>>expression
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Lambdaexpressionthatspecifiestheexpectedpropertygetter.
TypeParametersTProperty
Typeoftheproperty.Typicallyomittedasitcanbeinferredfromtheexpression.
RemarksIfmorethanoneexpectationissetforthesamepropertygetter,thelatestonewinsandistheonethatwillbeexecuted.
Copy
ExamplesC#
mock.ExpectGet(x=>x.Suspended)
.Returns(true);
SeeAlsoIMock<(Of<(T>)>)InterfaceMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIMock<(Of<(T>)>)..::.ExpectSetMethodIMock<(Of<(T>)>)InterfaceSeeAlsoSendFeedback
OverloadList Name Description
ExpectSet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>))
Setsanexpectationonthemockedtypeforacalltotoapropertysetter.
ExpectSet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>),TProperty)
Setsanexpectationonthemockedtypeforacalltotoapropertysetterwithaspecificvalue.
SeeAlsoIMock<(Of<(T>)>)InterfaceIMock<(Of<(T>)>)MembersMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.ExpectSet<(Of<(TProperty>)>)Method(<(Of<(<(Of<(T,TProperty>)>)>)>))IMock<(Of<(T>)>)InterfaceExampleSeeAlsoSendFeedbackSetsanexpectationonthemockedtypeforacalltotoapropertysetter.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IExpectSetter<TProperty>ExpectSet<TProperty>(
Expression<Func<T,TProperty>>expression
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Lambdaexpressionthatspecifiestheexpectedpropertysetter.
TypeParametersTProperty
Typeoftheproperty.Typicallyomittedasitcanbeinferredfromtheexpression.
RemarksIfmorethanoneexpectationissetforthesamepropertysetter,thelatestonewinsandistheonethatwillbeexecuted.
Copy
ExamplesC#
mock.ExpectSet(x=>x.Suspended);
SeeAlsoIMock<(Of<(T>)>)InterfaceExpectSetOverloadMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.ExpectSet<(Of<(TProperty>)>)Method(<(Of<(<(Of<(T,TProperty>)>)>)>),TProperty)IMock<(Of<(T>)>)InterfaceExampleSeeAlsoSendFeedbackSetsanexpectationonthemockedtypeforacalltotoapropertysetterwithaspecificvalue.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IExpectSetter<TProperty>ExpectSet<TProperty>(
Expression<Func<T,TProperty>>expression,
TPropertyvalue
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Lambdaexpressionthatspecifiestheexpectedpropertysetter.
valueType:TPropertyThevalueexpectedtobesetfortheproperty.
TypeParametersTProperty
Typeoftheproperty.Typicallyomittedasitcanbeinferredfromtheexpression.
RemarksMorethanoneexpectationcanbesetforthesetterwithdifferentvalues.
Copy
ExamplesC#
mock.ExpectSet(x=>x.Suspended,true);
SeeAlsoIMock<(Of<(T>)>)InterfaceExpectSetOverloadMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIMock<(Of<(T>)>)..::.VerifyMethodIMock<(Of<(T>)>)InterfaceSeeAlsoSendFeedback
OverloadList Name Description
Verify()()() ImplementsVerify()()().
Verify(Expression<(Of<(Action<(Of<(T>)>)>)>))
Verifiesthataspecificinvocationmatchingthegivenexpressionwasperformedonthemock.UseinconjuntionwiththedefaultLoose.
Verify<(Of<(TResult>)>)(Expression<(Of<(Func<(Of<(T,TResult>)>)>)>))
Verifiesthataspecificinvocationmatchingthegivenexpressionwasperformedonthemock.UseinconjuntionwiththedefaultLoose.
SeeAlsoIMock<(Of<(T>)>)InterfaceIMock<(Of<(T>)>)MembersMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.VerifyMethodIMock<(Of<(T>)>)InterfaceSeeAlsoSendFeedbackImplementsVerify()()().
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
voidVerify()
SeeAlsoIMock<(Of<(T>)>)InterfaceVerifyOverloadMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.VerifyMethod(<(Of<(<(Of<(T>)>)>)>))IMock<(Of<(T>)>)InterfaceExampleSeeAlsoSendFeedbackVerifiesthataspecificinvocationmatchingthegivenexpressionwasperformedonthemock.UseinconjuntionwiththedefaultLoose.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
voidVerify(
Expression<Action<T>>expression
)
Parameters
expressionType:Expression<(Of<(Action<(Of<(T>)>)>)>)Expressiontoverify.
Copy
ExamplesThisexampleassumesthatthemockhasbeenused,andlaterwewanttoverifythatagiveninvocationwithspecificparameterswasperformed:
C#
varmock=newMock<IProcessor>();
//exercisemock
//...
//Willthrowifthetestcodedidn'tcallExecutewitha"ping"stringargument.
mock.Verify(proc=>proc.Execute("ping"));
ExceptionsException Condition
Moq..::.MockException Theinvocationwasnotperformedonthemock.
SeeAlsoIMock<(Of<(T>)>)InterfaceVerifyOverloadMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.Verify<(Of<(TResult>)>)Method(<(Of<(<(Of<(T,TResult>)>)>)>))IMock<(Of<(T>)>)InterfaceExampleSeeAlsoSendFeedbackVerifiesthataspecificinvocationmatchingthegivenexpressionwasperformedonthemock.UseinconjuntionwiththedefaultLoose.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
voidVerify<TResult>(
Expression<Func<T,TResult>>expression
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TResult>)>)>)>)Expressiontoverify.
TypeParametersTResult
Typeofreturnvaluefromtheexpression.
Copy
ExamplesThisexampleassumesthatthemockhasbeenused,andlaterwewanttoverifythatagiveninvocationwithspecificparameterswasperformed:
C#
varmock=newMock<IWarehouse>();
//exercisemock
//...
//Willthrowifthetestcodedidn'tcallHasInventory.
mock.Verify(warehouse=>warehouse.HasInventory(TALISKER,50));
ExceptionsException Condition
Moq..::.MockException Theinvocationwasnotperformedonthemock.
SeeAlsoIMock<(Of<(T>)>)InterfaceVerifyOverloadMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.VerifyAllMethodIMock<(Of<(T>)>)InterfaceSeeAlsoSendFeedbackImplementsVerifyAll()()().
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
voidVerifyAll()
SeeAlsoIMock<(Of<(T>)>)InterfaceMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.VerifyGet<(Of<(TProperty>)>)MethodIMock<(Of<(T>)>)InterfaceExampleSeeAlsoSendFeedbackVerifiesthatapropertywasreadonthemock.UseinconjuntionwiththedefaultLoose.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
voidVerifyGet<TProperty>(
Expression<Func<T,TProperty>>expression
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Expressiontoverify.
TypeParametersTProperty
Typeofthepropertytoverify.Typicallyomittedasitcanbeinferredfromtheexpression'sreturntype.
Copy
ExamplesThisexampleassumesthatthemockhasbeenused,andlaterwewanttoverifythatagivenpropertywasretrievedfromit:
C#
varmock=newMock<IWarehouse>();
//exercisemock
//...
//Willthrowifthetestcodedidn'tretrievetheIsClosedproperty.
mock.VerifyGet(warehouse=>warehouse.IsClosed);
ExceptionsException Condition
Moq..::.MockException Theinvocationwasnotperformedonthemock.
SeeAlsoIMock<(Of<(T>)>)InterfaceMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIMock<(Of<(T>)>)..::.VerifySetMethodIMock<(Of<(T>)>)InterfaceSeeAlsoSendFeedback
OverloadList Name Description
VerifySet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>))
Verifiesthatapropertyhasbeensetonthemock.UseinconjuntionwiththedefaultLoose.
VerifySet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>),TProperty)
Verifiesthatapropertyhasbeensetonthemocktothegivenvalue.UseinconjuntionwiththedefaultLoose.
SeeAlsoIMock<(Of<(T>)>)InterfaceIMock<(Of<(T>)>)MembersMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.VerifySet<(Of<(TProperty>)>)Method(<(Of<(<(Of<(T,TProperty>)>)>)>))IMock<(Of<(T>)>)InterfaceExampleSeeAlsoSendFeedbackVerifiesthatapropertyhasbeensetonthemock.UseinconjuntionwiththedefaultLoose.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
voidVerifySet<TProperty>(
Expression<Func<T,TProperty>>expression
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Expressiontoverify.
TypeParametersTProperty
Typeofthepropertytoverify.Typicallyomittedasitcanbeinferredfromtheexpression'sreturntype.
Copy
ExamplesThisexampleassumesthatthemockhasbeenused,andlaterwewanttoverifythatagiveninvocationwithspecificparameterswasperformed:
C#
varmock=newMock<IWarehouse>();
//exercisemock
//...
//Willthrowifthetestcodedidn'tsettheIsClosedproperty.
mock.VerifySet(warehouse=>warehouse.IsClosed);
ExceptionsException Condition
Moq..::.MockException Theinvocationwasnotperformedonthemock.
SeeAlsoIMock<(Of<(T>)>)InterfaceVerifySetOverloadMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.VerifySet<(Of<(TProperty>)>)Method(<(Of<(<(Of<(T,TProperty>)>)>)>),TProperty)IMock<(Of<(T>)>)InterfaceExampleSeeAlsoSendFeedbackVerifiesthatapropertyhasbeensetonthemocktothegivenvalue.UseinconjuntionwiththedefaultLoose.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
voidVerifySet<TProperty>(
Expression<Func<T,TProperty>>expression,
TPropertyvalue
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Expressiontoverify.
valueType:TPropertyThevaluethatshouldhavebeensetontheproperty.
TypeParametersTProperty
Typeofthepropertytoverify.Typicallyomittedasitcanbeinferredfromtheexpression'sreturntype.
Copy
ExamplesThisexampleassumesthatthemockhasbeenused,andlaterwewanttoverifythatagiveninvocationwithspecificparameterswasperformed:
C#
varmock=newMock<IWarehouse>();
//exercisemock
//...
//Willthrowifthetestcodedidn'tsettheIsClosedpropertytotrue
mock.VerifySet(warehouse=>warehouse.IsClosed,true);
ExceptionsException Condition
Moq..::.MockException Theinvocationwasnotperformedonthemock.
SeeAlsoIMock<(Of<(T>)>)InterfaceVerifySetOverloadMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIMock<(Of<(T>)>)PropertiesIMock<(Of<(T>)>)InterfaceSeeAlsoSendFeedback
TheIMock<(Of<(T>)>)typeexposesthefollowingmembers.
Properties Name Description
Behavior Behaviorofthemock,accordingtothevaluesetintheconstructor.
DefaultValue Specifiesthebehaviortousewhenreturningdefaultvaluesforunexpectedinvocations.Object Exposesthemockedobjectinstance.
SeeAlsoIMock<(Of<(T>)>)InterfaceMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.BehaviorPropertyIMock<(Of<(T>)>)InterfaceSeeAlsoSendFeedbackBehaviorofthemock,accordingtothevaluesetintheconstructor.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
MockBehaviorBehavior{get;}
SeeAlsoIMock<(Of<(T>)>)InterfaceMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.DefaultValuePropertyIMock<(Of<(T>)>)InterfaceSeeAlsoSendFeedbackSpecifiesthebehaviortousewhenreturningdefaultvaluesforunexpectedinvocations.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
DefaultValueDefaultValue{get;set;}
SeeAlsoIMock<(Of<(T>)>)InterfaceMoqNamespace
C#
MoqIMock<(Of<(T>)>)..::.ObjectPropertyIMock<(Of<(T>)>)InterfaceSeeAlsoSendFeedbackExposesthemockedobjectinstance.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
TObject{get;}
SeeAlsoIMock<(Of<(T>)>)InterfaceMoqNamespace
C#
MoqItClassMembersSeeAlsoSendFeedbackAllowsthespecificationofamatchingconditionforanargumentinamethodinvocation,ratherthanaspecificargumentvalue."It"referstotheargumentbeingmatched.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticclassIt
RemarksThisclassallowstheexpectationtomatchamethodinvocationwithanarbitraryvalue,withavalueinaspecifiedrange,orevenonethatmatchesagivenpredicate.
InheritanceHierarchyObjectMoq..::.It
C#IncludeProtectedMembersIncludeInheritedMembers
MoqItMembersItClassMethodsSeeAlsoSendFeedback
TheIttypeexposesthefollowingmembers.
Methods Name Description
Is<(Of<(TValue>)>) Matchesanyvaluethatsatisfiesthegivenpredicate.
IsAny<(Of<(TValue>)>) MatchesanyvalueofthegivenTValuetype.IsInRange<(Of<(TValue>)>)
Matchesanyvaluethatisintherangespecified.
IsRegex Overloaded.
C#IncludeProtectedMembersIncludeInheritedMembers
MoqItMethodsItClassSeeAlsoSendFeedback
TheIttypeexposesthefollowingmembers.
Methods Name Description
Is<(Of<(TValue>)>) Matchesanyvaluethatsatisfiesthegivenpredicate.
IsAny<(Of<(TValue>)>) MatchesanyvalueofthegivenTValuetype.IsInRange<(Of<(TValue>)>)
Matchesanyvaluethatisintherangespecified.
IsRegex Overloaded.
C#
MoqIt..::.Is<(Of<(TValue>)>)MethodItClassExampleSeeAlsoSendFeedbackMatchesanyvaluethatsatisfiesthegivenpredicate.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticTValueIs<TValue>(
Expression<Predicate<TValue>>match
)
Parameters
matchType:Expression<(Of<(Predicate<(Of<(TValue>)>)>)>)Thepredicateusedtomatchthemethodargument.
TypeParametersTValue
Typeoftheargumenttocheck.
RemarksAllowsthespecificationofapredicatetoperformmatchingofmethodcallarguments.
Copy
Copy
ExamplesThisexampleshowshowtoreturnthevalue1whenevertheargumenttotheDomethodisanevennumber.
C#
mock.Expect(x=>x.Do(It.Is<int>(i=>i%2==0)))
.Returns(1);
Thisexampleshowshowtothrowanexceptioniftheargumenttothemethodisanegativenumber:
C#
mock.Expect(x=>x.GetUser(It.Is<int>(i=>i<0)))
.Throws(newArgumentException());
C#
MoqIt..::.IsAny<(Of<(TValue>)>)MethodItClassExampleSeeAlsoSendFeedbackMatchesanyvalueofthegivenTValuetype.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticTValueIsAny<TValue>()
TypeParametersTValue
Typeofthevalue.
RemarksTypicallyusedwhentheactualargumentvalueforamethodcallisnotrelevant.
Copy
ExamplesC#
//ThrowsanexceptionforacalltoRemovewithanystringvalue.
mock.Expect(x=>x.Remove(It.IsAny<string>())).Throws(newInvalidOperationException());
C#
MoqIt..::.IsInRange<(Of<(TValue>)>)MethodItClassExampleSeeAlsoSendFeedbackMatchesanyvaluethatisintherangespecified.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticTValueIsInRange<TValue>(
TValuefrom,
TValueto,
RangerangeKind
)
whereTValue:IComparable
Parameters
fromType:TValueThelowerboundoftherange.
toType:TValueTheupperboundoftherange.
rangeKindType:Moq..::.RangeThekindofrange.SeeRange.
TypeParametersTValue
Typeoftheargumenttocheck.
Copy
ExamplesThefollowingexampleshowshowtoexpectamethodcallwithanintegerargumentwithinthe0..100range.
C#
mock.Expect(x=>x.HasInventory(
It.IsAny<string>(),
It.IsInRange(0,100,Range.Inclusive)))
.Returns(false);
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIt..::.IsRegexMethodItClassSeeAlsoSendFeedback
OverloadList Name Description
IsRegex(String) Matchesastringargumentifitmatchesthegivenregularexpressionpattern.
IsRegex(String,RegexOptions)
Matchesastringargumentifitmatchesthegivenregularexpressionpattern.
C#
MoqIt..::.IsRegexMethod()ItClassExampleSeeAlsoSendFeedbackMatchesastringargumentifitmatchesthegivenregularexpressionpattern.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticstringIsRegex(
stringregex
)
Parameters
regexType:StringThepatterntousetomatchthestringargumentvalue.
Copy
ExamplesThefollowingexampleshowshowtoexpectacalltoamethodwherethestringargumentmatchesthegivenregularexpression:
C#
mock.Expect(x=>x.Check(It.IsRegex("[a-z]+"))).Returns(1);
SeeAlsoItClassIsRegexOverloadMoqNamespace
C#
MoqIt..::.IsRegexMethod(,)ItClassExampleSeeAlsoSendFeedbackMatchesastringargumentifitmatchesthegivenregularexpressionpattern.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticstringIsRegex(
stringregex,
RegexOptionsoptions
)
Parameters
regexType:StringThepatterntousetomatchthestringargumentvalue.
optionsType:RegexOptionsTheoptionsusedtointerpretthepattern.
Copy
ExamplesThefollowingexampleshowshowtoexpectacalltoamethodwherethestringargumentmatchesthegivenregularexpression,inacaseinsensitiveway:
C#
mock.Expect(x=>x.Check(It.IsRegex("[a-z]+",RegexOptions.IgnoreCase))).Returns(
SeeAlsoItClassIsRegexOverloadMoqNamespace
C#
MoqMatcherAttributeClassMembersExampleSeeAlsoSendFeedbackMarksamethodasamatcher,whichallowscompletereplacementofthebuilt-inItclasswithyourownargumentmatchingrules.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicclassMatcherAttribute:Attribute
Copy
Copy
RemarksTheargumentmatchingisusedtodeterminewhetheraconcreteinvocationinthemockmatchesagivenexpectation.Thismatchingmechanismisfullyextensible.
Therearetwopartsofamatcher:thecompilermatcherandtheruntimematcher.
CompilermatcherUsedtosatisfythecompilerrequirementsfortheargument.Needstobeamethodoptionallyreceivinganyargumentsyoumightneedforthematching,butwithareturntypethatmatchesthatoftheargument.
Let'ssayIwanttomatchalistsofordersthatcontainsaparticularone.Imightcreateacompilermatcherlikethefollowing:
C#
publicstaticclassOrders
{
[Matcher]
publicstaticIEnumerable<Order>Contains(Orderorder)
{
returnnull;
}
}
Nowwecaninvokethisstaticmethodinsteadofanargumentinaninvocation:
C#
varorder=newOrder{...};
varmock=newMock<IRepository<Order>>();
mock.Expect(x=>x.Save(Orders.Contains(order)))
.Throws<ArgumentException>();
Notethatthereturnvaluefromthecompilermatcherisirrelevant.Thismethodwillneverbecalled,andisjustusedtosatisfythecompilerandtosignalMoqthatthisisnotamethodthatwewanttobeinvokedatruntime.
Copy
RuntimematcherTheruntimematcheristheonethatwillactuallyperformevaluationwhenthetestisrun,andisdefinedbyconventiontohavethesamesignatureasthecompilermatcher,butwherethereturnvalueisthefirstargumenttothecall,whichcontainstheobjectreceivedbytheactualinvocationatruntime:
C#
publicstaticboolContains(IEnumerable<Order>orders,Orderorder)
{
returnorders.Contains(order);
}
Atruntime,themockedmethodwillbeinvokedwithaspecificlistoforders.Thisvaluewillbepassedtothisruntimematcherasthefirstargument,whilethesecondargumentistheonespecifiedintheexpectation(x.Save(Orders.Contains(order))).
Thebooleanreturneddetermineswhetherthegivenargumenthasbeenmatched.Ifallargumentstotheexpectedmethodarematched,thentheexpectationisverified.
Usingthisextensibleinfrastructure,youcaneasilyreplacetheentireItsetofmatcherswithyourown.Youcanalsoavoidthetypical(andannoying)lengthyexpressionsthatresultwhenyouhavemultipleargumentsthatusegenerics.
Copy
Copy
ExamplesThefollowingisthecompleteexampleexplainedabove:
C#
publicstaticclassOrders
{
[Matcher]
publicstaticIEnumerable<Order>Contains(Orderorder)
{
returnnull;
}
publicstaticboolContains(IEnumerable<Order>orders,Orderorder)
{
returnorders.Contains(order);
}
}
Andtheconcretetestusingthismatcher:
C#
varorder=newOrder{...};
varmock=newMock<IRepository<Order>>();
mock.Expect(x=>x.Save(Orders.Contains(order)))
.Throws<ArgumentException>();
//usemock,invokeSave,andhavethematcherfilter.
InheritanceHierarchyObjectAttributeMoq..::.MatcherAttribute
SeeAlsoMatcherAttributeMembersMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMatcherAttributeMembersMatcherAttributeClassConstructorsSeeAlsoSendFeedback
TheMatcherAttributetypeexposesthefollowingmembers.
Constructors Name Description
MatcherAttribute InitializesanewinstanceoftheMatcherAttributeclass
SeeAlsoMatcherAttributeClassMoqNamespace
C#
MoqMatcherAttributeConstructorMatcherAttributeClassSeeAlsoSendFeedbackInitializesanewinstanceoftheMatcherAttributeclass
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMatcherAttribute()
SeeAlsoMatcherAttributeClassMoqNamespace
C#
MoqMockClassMembersSeeAlsoSendFeedbackBaseclassformocksandstatichelperclasswithmethodsthatapplytomockedobjects,suchasGet<(Of<(T>)>)(T)toretrieveaMock<(Of<(T>)>)fromanobjectinstance.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicabstractclassMock:IMock
InheritanceHierarchyObjectMoq..::.MockMoq..::.Mock<(Of<(T>)>)
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockMembersMockClassConstructorsMethodsPropertiesSeeAlsoSendFeedback
TheMocktypeexposesthefollowingmembers.
Constructors Name DescriptionMock Initializesthemock
Methods Name Description
CreateEventHandler Overloaded.
Get<(Of<(T>)>) Retrievesthemockobjectforthegivenobjectinstance.
GetObject Returnsthemockedobjectvalue.Verify ImplementsVerify()()().VerifyAll ImplementsVerifyAll()()().
Properties Name Description
Behavior ImplementsBehavior.CallBase ImplementsCallBase.DefaultValue ImplementsDefaultValue.
ImplementedInterfaces Exposesthelistofextrainterfacesimplementedbythemock.
Object Themockedobjectinstance.ImplementsObject.
C#
MoqMockConstructorMockClassSeeAlsoSendFeedbackInitializesthemock
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
protectedMock()
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockMethodsMockClassSeeAlsoSendFeedback
TheMocktypeexposesthefollowingmembers.
Methods Name Description
CreateEventHandler Overloaded.
Get<(Of<(T>)>) Retrievesthemockobjectforthegivenobjectinstance.
GetObject Returnsthemockedobjectvalue.Verify ImplementsVerify()()().VerifyAll ImplementsVerifyAll()()().
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMock..::.CreateEventHandlerMethodMockClassSeeAlsoSendFeedback
OverloadList Name Description
CreateEventHandler<(Of<(TEventArgs>)>)()()()
ImplementsCreateEventHandler<(Of<(TEventArgs>)>)()()().
CreateEventHandler()()() ImplementsCreateEventHandler()()()
SeeAlsoMockClassMockMembersMoqNamespace
C#
MoqMock..::.CreateEventHandler<(Of<(TEventArgs>)>)MethodMockClassSeeAlsoSendFeedbackImplementsCreateEventHandler<(Of<(TEventArgs>)>)()()().
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMockedEvent<TEventArgs>CreateEventHandler<TEventArgs>()
whereTEventArgs:EventArgs
TypeParametersTEventArgs
Typeofeventargumentclass.
SeeAlsoMockClassCreateEventHandlerOverloadMoqNamespace
C#
MoqMock..::.CreateEventHandlerMethodMockClassSeeAlsoSendFeedbackImplementsCreateEventHandler()()()
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMockedEvent<EventArgs>CreateEventHandler()
SeeAlsoMockClassCreateEventHandlerOverloadMoqNamespace
C#
MoqMock..::.Get<(Of<(T>)>)MethodMockClassExampleSeeAlsoSendFeedbackRetrievesthemockobjectforthegivenobjectinstance.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticIMock<T>Get<T>(
Tmocked
)
whereT:class
Parameters
mockedType:TTheinstanceofthemockedobject.
TypeParametersT
Typeofthemocktoretrieve.Canbeomittedasit'sinferredfromtheobjectinstancepassedinasthemockedinstance.
ReturnValue
Themockassociatedwiththemockedobject.
Copy
ExamplesThefollowingexampleshowshowtoaddanewexpectationtoanobjectinstancewhichisnottheoriginalMock<(Of<(T>)>)butrathertheobjectassociatedwithit:
C#
//Typedinstance,notthemock,isretrievedfromsometestAPI.
HttpContextBasecontext=GetMockContext();
//context.Requestisthetypedobjectfromthe"real"API
//soinordertoaddanexpectationtoit,weneedtoget
//themockthat"owns"it
Mock<HttpRequestBase>request=Mock.Get(context.Request);
mock.Expect(req=>req.AppRelativeCurrentExecutionFilePath)
.Returns(tempUrl);
ExceptionsException Condition
ArgumentException ThereceivedmockedinstancewasnotcreatedbyMoq.
C#
MoqMock..::.GetObjectMethodMockClassSeeAlsoSendFeedbackReturnsthemockedobjectvalue.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
protectedabstractObjectGetObject()
C#
MoqMock..::.VerifyMethodMockClassSeeAlsoSendFeedbackImplementsVerify()()().
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicabstractvoidVerify()
Implements
IMock..::.Verify()()()
C#
MoqMock..::.VerifyAllMethodMockClassSeeAlsoSendFeedbackImplementsVerifyAll()()().
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicabstractvoidVerifyAll()
Implements
IMock..::.VerifyAll()()()
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockPropertiesMockClassSeeAlsoSendFeedback
TheMocktypeexposesthefollowingmembers.
Properties Name Description
Behavior ImplementsBehavior.CallBase ImplementsCallBase.DefaultValue ImplementsDefaultValue.
ImplementedInterfaces Exposesthelistofextrainterfacesimplementedbythemock.
Object Themockedobjectinstance.ImplementsObject.
C#
MoqMock..::.BehaviorPropertyMockClassSeeAlsoSendFeedbackImplementsBehavior.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMockBehaviorBehavior{get;internalset;}
C#
MoqMock..::.CallBasePropertyMockClassSeeAlsoSendFeedbackImplementsCallBase.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicboolCallBase{get;set;}
Implements
IMock..::.CallBase
C#
MoqMock..::.DefaultValuePropertyMockClassSeeAlsoSendFeedbackImplementsDefaultValue.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicDefaultValueDefaultValue{get;set;}
Implements
IMock..::.DefaultValue
C#
MoqMock..::.ImplementedInterfacesPropertyMockClassSeeAlsoSendFeedbackExposesthelistofextrainterfacesimplementedbythemock.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
protectedinternalList<Type>ImplementedInterfaces{get;privateset
C#
MoqMock..::.ObjectPropertyMockClassSeeAlsoSendFeedbackThemockedobjectinstance.ImplementsObject.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicObjectObject{get;}
Implements
IMock..::.Object
C#
MoqMock<(Of<(T>)>)ClassMembersSeeAlsoSendFeedbackCoreimplementationoftheIMock<(Of<(T>)>)interface.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicclassMock<T>:Mock,IMock<T>
whereT:class
TypeParametersT
Typetomock.
InheritanceHierarchyObjectMoq..::.MockMoq..::.Mock<(Of<(T>)>)
SeeAlsoMock<(Of<(T>)>)MembersMoqNamespaceMoq..::.IMock<(Of<(T>)>)
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMock<(Of<(T>)>)MembersMock<(Of<(T>)>)ClassConstructorsMethodsPropertiesSeeAlsoSendFeedback
TheMock<(Of<(T>)>)typeexposesthefollowingmembers.
Constructors Name DescriptionMock<(Of<(T>)>) Overloaded.
Methods Name Description
As<(Of<(TInterface>)>) ImplementsAs<(Of<(TInterface>)>)()()().
CreateEventHandler<(Of<(TEventArgs>)>)
ImplementsCreateEventHandler<(Of<(TEventArgs>)>)()()().(InheritedfromMock.)
Expect Overloaded.
ExpectGet<(Of<(TProperty>)>)
ImplementsExpectGet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)).
ExpectSet Overloaded.
GetObject Returnsthemockedobjectvalue.(OverridesMock..::.GetObject()()().)
Verify Overloaded.
VerifyAll ImplementsVerifyAll()()().(OverridesMock..::.VerifyAll()()().)
VerifyGet<(Of<(TProperty>)>)
ImplementsVerifyGet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)).
VerifySet Overloaded.
Properties Name Description
Behavior ImplementsBehavior.(InheritedfromMock.)
CallBase ImplementsCallBase.(InheritedfromMock.)
DefaultValue ImplementsDefaultValue.(InheritedfromMock.)
ImplementedInterfacesExposesthelistofextrainterfacesimplementedbythemock.(InheritedfromMock.)
Object Exposesthemockedobjectinstance.
SeeAlsoMock<(Of<(T>)>)ClassMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMock<(Of<(T>)>)ConstructorMock<(Of<(T>)>)ClassSeeAlsoSendFeedback
OverloadList Name Description
Mock<(Of<(T>)>)()()() Initializesaninstanceofthemockwithdefaultbehavior.
Mock<(Of<(T>)>)(MockBehavior)
Initializesaninstanceofthemockwiththespecifiedbehavior.
Mock<(Of<(T>)>)(array<Object>[]()[])
Initializesaninstanceofthemockwithdefaultbehaviorandwiththegivenconstructorargumentsfortheclass.(OnlyvalidwhenTisaclass)
Mock<(Of<(T>)>)(MockBehavior,array<Object>[]()[])
Initializesaninstanceofthemockwithaspecificbehaviorwiththegivenconstructorargumentsfortheclass.
SeeAlsoMock<(Of<(T>)>)ClassMock<(Of<(T>)>)MembersMoqNamespace
C#
MoqMock<(Of<(T>)>)ConstructorMock<(Of<(T>)>)ClassExampleSeeAlsoSendFeedbackInitializesaninstanceofthemockwithdefaultbehavior.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMock()
Copy
ExamplesC#
varmock=newMock<IFormatProvider>();
SeeAlsoMock<(Of<(T>)>)ClassMock<(Of<(T>)>)OverloadMoqNamespace
C#
MoqMock<(Of<(T>)>)Constructor(MockBehavior)Mock<(Of<(T>)>)ClassExampleSeeAlsoSendFeedbackInitializesaninstanceofthemockwiththespecifiedbehavior.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMock(
MockBehaviorbehavior
)
Parameters
behaviorType:Moq..::.MockBehaviorBehaviorofthemock.
Copy
ExamplesC#
varmock=newMock<IFormatProvider>(MockBehavior.Relaxed);
SeeAlsoMock<(Of<(T>)>)ClassMock<(Of<(T>)>)OverloadMoqNamespace
C#
MoqMock<(Of<(T>)>)Constructor(array<>[]()[])Mock<(Of<(T>)>)ClassExampleSeeAlsoSendFeedbackInitializesaninstanceofthemockwithdefaultbehaviorandwiththegivenconstructorargumentsfortheclass.(OnlyvalidwhenTisaclass)
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMock(
paramsObject[]args
)
Parameters
argsType:array<Object>[]()[]Optionalconstructorargumentsifthemockedtypeisaclass.
RemarksThemockwilltrytofindthebestmatchconstructorgiventheconstructorarguments,andinvokethattoinitializetheinstance.Thisappliesonlyforclasses,notinterfaces.
Copy
ExamplesC#
varmock=newMock<MyProvider>(someArgument,25);
SeeAlsoMock<(Of<(T>)>)ClassMock<(Of<(T>)>)OverloadMoqNamespace
C#
MoqMock<(Of<(T>)>)Constructor(MockBehavior,array<>[]()[])Mock<(Of<(T>)>)ClassExampleSeeAlsoSendFeedbackInitializesaninstanceofthemockwithaspecificbehaviorwiththegivenconstructorargumentsfortheclass.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMock(
MockBehaviorbehavior,
paramsObject[]args
)
Parameters
behaviorType:Moq..::.MockBehaviorBehaviorofthemock.
argsType:array<Object>[]()[]Optionalconstructorargumentsifthemockedtypeisaclass.
RemarksThemockwilltrytofindthebestmatchconstructorgiventheconstructorarguments,andinvokethattoinitializetheinstance.Thisappliesonlytoclasses,notinterfaces.
Copy
ExamplesC#
varmock=newMock<MyProvider>(someArgument,25);
SeeAlsoMock<(Of<(T>)>)ClassMock<(Of<(T>)>)OverloadMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMock<(Of<(T>)>)MethodsMock<(Of<(T>)>)ClassSeeAlsoSendFeedback
TheMock<(Of<(T>)>)typeexposesthefollowingmembers.
Methods Name Description
As<(Of<(TInterface>)>) ImplementsAs<(Of<(TInterface>)>)()()().
CreateEventHandler<(Of<(TEventArgs>)>)
ImplementsCreateEventHandler<(Of<(TEventArgs>)>)()()().(InheritedfromMock.)
Expect Overloaded.
ExpectGet<(Of<(TProperty>)>)
ImplementsExpectGet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)).
ExpectSet Overloaded.
GetObject Returnsthemockedobjectvalue.(OverridesMock..::.GetObject()()().)
Verify Overloaded.
VerifyAll ImplementsVerifyAll()()().(OverridesMock..::.VerifyAll()()().)
VerifyGet<(Of<(TProperty>)>)
ImplementsVerifyGet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)).
VerifySet Overloaded.
SeeAlsoMock<(Of<(T>)>)ClassMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.As<(Of<(TInterface>)>)MethodMock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsAs<(Of<(TInterface>)>)()()().
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicvirtualIMock<TInterface>As<TInterface>()
whereTInterface:class
TypeParametersTInterface
Typeofinterfacetocastthemockto.
SeeAlsoMock<(Of<(T>)>)ClassMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMock<(Of<(T>)>)..::.ExpectMethodMock<(Of<(T>)>)ClassSeeAlsoSendFeedback
OverloadList Name Description
Expect(Expression<(Of<(Action<(Of<(T>)>)>)>))
ImplementsExpect(Expression<(Of<(Action<(Of<(T>)>)>)>)).
Expect<(Of<(TResult>)>)(Expression<(Of<(Func<(Of<(T,TResult>)>)>)>))
ImplementsExpect<(Of<(TResult>)>)(Expression<(Of<(Func<(Of<(T,TResult>)>)>)>)).
SeeAlsoMock<(Of<(T>)>)ClassMock<(Of<(T>)>)MembersMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.ExpectMethod(<(Of<(<(Of<(T>)>)>)>))Mock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsExpect(Expression<(Of<(Action<(Of<(T>)>)>)>)).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicIExpectExpect(
Expression<Action<T>>expression
)
Parameters
expressionType:Expression<(Of<(Action<(Of<(T>)>)>)>)Lambdaexpressionthatspecifiestheexpectedmethodinvocation.
Implements
IMock<(Of<(T>)>)..::.Expect<(Of<(TResult>)>)(Expression<(Of<(Func<(Of<(T,TResult>)>)>)>))
SeeAlsoMock<(Of<(T>)>)ClassExpectOverloadMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.Expect<(Of<(TResult>)>)Method(<(Of<(<(Of<(T,TResult>)>)>)>))Mock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsExpect<(Of<(TResult>)>)(Expression<(Of<(Func<(Of<(T,TResult>)>)>)>)).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicIExpect<TResult>Expect<TResult>(
Expression<Func<T,TResult>>expression
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TResult>)>)>)>)Lambdaexpressionthatspecifiestheexpectedmethodinvocation.
TypeParametersTResult
Typeofthereturnvalue.Typicallyomittedasitcanbeinferredfromtheexpression.
SeeAlsoMock<(Of<(T>)>)ClassExpectOverloadMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.ExpectGet<(Of<(TProperty>)>)MethodMock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsExpectGet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicIExpectGetter<TProperty>ExpectGet<TProperty>(
Expression<Func<T,TProperty>>expression
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Lambdaexpressionthatspecifiestheexpectedpropertygetter.
TypeParametersTProperty
Typeoftheproperty.Typicallyomittedasitcanbeinferredfromtheexpression.
SeeAlsoMock<(Of<(T>)>)ClassMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMock<(Of<(T>)>)..::.ExpectSetMethodMock<(Of<(T>)>)ClassSeeAlsoSendFeedback
OverloadList Name Description
ExpectSet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>))
ImplementsExpectSet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)).
ExpectSet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>),TProperty)
ImplementsExpectSet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>),TProperty).
SeeAlsoMock<(Of<(T>)>)ClassMock<(Of<(T>)>)MembersMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.ExpectSet<(Of<(TProperty>)>)Method(<(Of<(<(Of<(T,TProperty>)>)>)>))Mock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsExpectSet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicIExpectSetter<TProperty>ExpectSet<TProperty>(
Expression<Func<T,TProperty>>expression
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Lambdaexpressionthatspecifiestheexpectedpropertysetter.
TypeParametersTProperty
Typeoftheproperty.Typicallyomittedasitcanbeinferredfromtheexpression.
SeeAlsoMock<(Of<(T>)>)ClassExpectSetOverloadMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.ExpectSet<(Of<(TProperty>)>)Method(<(Of<(<(Of<(T,TProperty>)>)>)>),TProperty)Mock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsExpectSet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>),TProperty).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicIExpectSetter<TProperty>ExpectSet<TProperty>(
Expression<Func<T,TProperty>>expression,
TPropertyvalue
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Lambdaexpressionthatspecifiestheexpectedpropertysetter.
valueType:TPropertyThevalueexpectedtobesetfortheproperty.
TypeParametersTProperty
Typeoftheproperty.Typicallyomittedasitcanbeinferredfromtheexpression.
SeeAlsoMock<(Of<(T>)>)ClassExpectSetOverloadMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.GetObjectMethodMock<(Of<(T>)>)ClassSeeAlsoSendFeedbackReturnsthemockedobjectvalue.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
protectedoverrideObjectGetObject()
SeeAlsoMock<(Of<(T>)>)ClassMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMock<(Of<(T>)>)..::.VerifyMethodMock<(Of<(T>)>)ClassSeeAlsoSendFeedback
OverloadList Name Description
Verify()()() ImplementsVerify()()().(OverridesMock..::.Verify()()().)
Verify(Expression<(Of<(Action<(Of<(T>)>)>)>))
ImplementsVerify(Expression<(Of<(Action<(Of<(T>)>)>)>)).
Verify<(Of<(TResult>)>)(Expression<(Of<(Func<(Of<(T,TResult>)>)>)>))
ImplementsVerify<(Of<(TResult>)>)(Expression<(Of<(Func<(Of<(T,TResult>)>)>)>)).
SeeAlsoMock<(Of<(T>)>)ClassMock<(Of<(T>)>)MembersMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.VerifyMethodMock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsVerify()()().
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicoverridevoidVerify()
Implements
IMock<(Of<(T>)>)..::.Verify()()()IMock..::.Verify()()()
SeeAlsoMock<(Of<(T>)>)ClassVerifyOverloadMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.VerifyMethod(<(Of<(<(Of<(T>)>)>)>))Mock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsVerify(Expression<(Of<(Action<(Of<(T>)>)>)>)).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicvirtualvoidVerify(
Expression<Action<T>>expression
)
Parameters
expressionType:Expression<(Of<(Action<(Of<(T>)>)>)>)Expressiontoverify.
Implements
IMock<(Of<(T>)>)..::.Verify(Expression<(Of<(Action<(Of<(T>)>)>)>))
SeeAlsoMock<(Of<(T>)>)ClassVerifyOverloadMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.Verify<(Of<(TResult>)>)Method(<(Of<(<(Of<(T,TResult>)>)>)>))Mock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsVerify<(Of<(TResult>)>)(Expression<(Of<(Func<(Of<(T,TResult>)>)>)>)).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicvirtualvoidVerify<TResult>(
Expression<Func<T,TResult>>expression
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TResult>)>)>)>)Expressiontoverify.
TypeParametersTResult
Typeofreturnvaluefromtheexpression.
Implements
IMock<(Of<(T>)>)..::.Verify(Expression<(Of<(Action<(Of<(T>)>)>)>))
SeeAlsoMock<(Of<(T>)>)ClassVerifyOverloadMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.VerifyAllMethodMock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsVerifyAll()()().
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicoverridevoidVerifyAll()
Implements
IMock<(Of<(T>)>)..::.VerifyAll()()()IMock..::.VerifyAll()()()
SeeAlsoMock<(Of<(T>)>)ClassMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.VerifyGet<(Of<(TProperty>)>)MethodMock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsVerifyGet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicvirtualvoidVerifyGet<TProperty>(
Expression<Func<T,TProperty>>expression
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Expressiontoverify.
TypeParametersTProperty
Typeofthepropertytoverify.Typicallyomittedasitcanbeinferredfromtheexpression'sreturntype.
Implements
IMock<(Of<(T>)>)..::.VerifyGet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>))
SeeAlsoMock<(Of<(T>)>)ClassMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMock<(Of<(T>)>)..::.VerifySetMethodMock<(Of<(T>)>)ClassSeeAlsoSendFeedback
OverloadList Name Description
VerifySet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>))
ImplementsVerifySet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)).
VerifySet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>),TProperty)
ImplementsVerifySet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>),TProperty).
SeeAlsoMock<(Of<(T>)>)ClassMock<(Of<(T>)>)MembersMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.VerifySet<(Of<(TProperty>)>)Method(<(Of<(<(Of<(T,TProperty>)>)>)>))Mock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsVerifySet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicvirtualvoidVerifySet<TProperty>(
Expression<Func<T,TProperty>>expression
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Expressiontoverify.
TypeParametersTProperty
Typeofthepropertytoverify.Typicallyomittedasitcanbeinferredfromtheexpression'sreturntype.
Implements
IMock<(Of<(T>)>)..::.VerifySet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>))
SeeAlsoMock<(Of<(T>)>)ClassVerifySetOverloadMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.VerifySet<(Of<(TProperty>)>)Method(<(Of<(<(Of<(T,TProperty>)>)>)>),TProperty)Mock<(Of<(T>)>)ClassSeeAlsoSendFeedbackImplementsVerifySet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>),TProperty).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicvirtualvoidVerifySet<TProperty>(
Expression<Func<T,TProperty>>expression,
TPropertyvalue
)
Parameters
expressionType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Expressiontoverify.
valueType:TPropertyThevaluethatshouldhavebeensetontheproperty.
TypeParametersTProperty
Typeofthepropertytoverify.Typicallyomittedasitcanbeinferredfromtheexpression'sreturntype.
Implements
IMock<(Of<(T>)>)..::.VerifySet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>),TProperty)
SeeAlsoMock<(Of<(T>)>)ClassVerifySetOverloadMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMock<(Of<(T>)>)PropertiesMock<(Of<(T>)>)ClassSeeAlsoSendFeedback
TheMock<(Of<(T>)>)typeexposesthefollowingmembers.
Properties Name Description
Behavior ImplementsBehavior.(InheritedfromMock.)
CallBase ImplementsCallBase.(InheritedfromMock.)
DefaultValue ImplementsDefaultValue.(InheritedfromMock.)
ImplementedInterfacesExposesthelistofextrainterfacesimplementedbythemock.(InheritedfromMock.)
Object Exposesthemockedobjectinstance.
SeeAlsoMock<(Of<(T>)>)ClassMoqNamespace
C#
MoqMock<(Of<(T>)>)..::.ObjectPropertyMock<(Of<(T>)>)ClassSeeAlsoSendFeedbackExposesthemockedobjectinstance.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicTObject{get;}
Implements
IMock<(Of<(T>)>)..::.Object
SeeAlsoMock<(Of<(T>)>)ClassMoqNamespace
C#
MoqMockBehaviorEnumerationSeeAlsoSendFeedbackOptionstocustomizethebehaviorofthemock.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicenumMockBehavior
MembersMembername Description
Strict Causesthemocktoalwaysthrowanexceptionforinvocationsthatdon'thaveacorrespondingexpectation.
LooseWillneverthrowexceptions,returningdefaultvalueswhennecessary(nullforreferencetypes,zeroforvaluetypesoremptyenumerablesandarrays).
Default Defaultmockbehavior,whichequalsLoose.
C#
MoqMockedEventClassMembersSeeAlsoSendFeedbackRepresentsagenericeventthathasbeenmockedandcanberised.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicclassMockedEvent
InheritanceHierarchyObjectMoq..::.MockedEventMoq..::.MockedEvent<(Of<(TEventArgs>)>)
SeeAlsoMockedEventMembersMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockedEventMembersMockedEventClassMethodsEventsSeeAlsoSendFeedback
TheMockedEventtypeexposesthefollowingmembers.
Operators Name Description
Implicit ProvidessupportforattachingaMockedEventtoagenericEventHandlerevent.
Events Name Description
Raised Eventraisedwheneverthemockedeventisrised.
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockedEventMethodsMockedEventClassSeeAlsoSendFeedback
TheMockedEventtypeexposesthefollowingmembers.
Operators Name Description
Implicit ProvidessupportforattachingaMockedEventtoagenericEventHandlerevent.
C#
MoqMockedEvent..::.ImplicitOperatorMockedEventClassSeeAlsoSendFeedbackProvidessupportforattachingaMockedEventtoagenericEventHandlerevent.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticimplicitoperatorEventHandler(
MockedEventmockEvent
)
Parameters
mockEventType:Moq..::.MockedEventEventtoconvert.
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockedEventEventsMockedEventClassSeeAlsoSendFeedback
TheMockedEventtypeexposesthefollowingmembers.
Events Name Description
Raised Eventraisedwheneverthemockedeventisrised.
C#
MoqMockedEvent..::.RaisedEventMockedEventClassSeeAlsoSendFeedbackEventraisedwheneverthemockedeventisrised.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publiceventEventHandlerRaised
C#
MoqMockedEvent<(Of<(TEventArgs>)>)ClassMembersSeeAlsoSendFeedbackProvidesatypedMockedEventforaspecifictypeofEventArgs.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicclassMockedEvent<TEventArgs>:MockedEvent
whereTEventArgs:EventArgs
TypeParametersTEventArgs
Thetypeofeventargumentsrequiredbytheevent.
RemarksThemockedeventcaneitherbeaEventHandlerorcustomeventhandlerwhichfollows.NETpracticeofprovidingobjectsender,EventArgsargskindofsignature.
InheritanceHierarchyObjectMoq..::.MockedEventMoq..::.MockedEvent<(Of<(TEventArgs>)>)
SeeAlsoMockedEvent<(Of<(TEventArgs>)>)MembersMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockedEvent<(Of<(TEventArgs>)>)MembersMockedEvent<(Of<(TEventArgs>)>)ClassMethodsEventsSeeAlsoSendFeedback
TheMockedEvent<(Of<(TEventArgs>)>)typeexposesthefollowingmembers.
Methods Name Description
Raise Raisestheassociatedeventwiththegiveneventargumentdata.
Operators Name Description
Implicit ProvidessupportforattachingaMockedEvent<(Of<(TEventArgs>)>)toagenericEventHandlerevent.
Events Name Description
Raised Eventraisedwheneverthemockedeventisrised.(InheritedfromMockedEvent.)
SeeAlsoMockedEvent<(Of<(TEventArgs>)>)ClassMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockedEvent<(Of<(TEventArgs>)>)MethodsMockedEvent<(Of<(TEventArgs>)>)ClassSeeAlsoSendFeedback
TheMockedEvent<(Of<(TEventArgs>)>)typeexposesthefollowingmembers.
Methods Name Description
Raise Raisestheassociatedeventwiththegiveneventargumentdata.
Operators Name Description
Implicit ProvidessupportforattachingaMockedEvent<(Of<(TEventArgs>)>)toagenericEventHandlerevent.
SeeAlsoMockedEvent<(Of<(TEventArgs>)>)ClassMoqNamespace
C#
MoqMockedEvent<(Of<(TEventArgs>)>)..::.ImplicitOperatorMockedEvent<(Of<(TEventArgs>)>)ClassSeeAlsoSendFeedbackProvidessupportforattachingaMockedEvent<(Of<(TEventArgs>)>)toagenericEventHandlerevent.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticimplicitoperatorEventHandler<TEventArgs>(
MockedEvent<TEventArgs>mockEvent
)
Parameters
mockEventType:Moq..::.MockedEvent<(Of<(TEventArgs>)>)Eventtoconvert.
SeeAlsoMockedEvent<(Of<(TEventArgs>)>)ClassMoqNamespace
C#
MoqMockedEvent<(Of<(TEventArgs>)>)..::.RaiseMethodMockedEvent<(Of<(TEventArgs>)>)ClassSeeAlsoSendFeedbackRaisestheassociatedeventwiththegiveneventargumentdata.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicvoidRaise(
TEventArgsargs
)
Parameters
argsType:TEventArgsDatatopasstotheevent.
SeeAlsoMockedEvent<(Of<(TEventArgs>)>)ClassMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockedEvent<(Of<(TEventArgs>)>)EventsMockedEvent<(Of<(TEventArgs>)>)ClassSeeAlsoSendFeedback
TheMockedEvent<(Of<(TEventArgs>)>)typeexposesthefollowingmembers.
Events Name Description
Raised Eventraisedwheneverthemockedeventisrised.(InheritedfromMockedEvent.)
SeeAlsoMockedEvent<(Of<(TEventArgs>)>)ClassMoqNamespace
C#
MoqMockExceptionClassMembersSeeAlsoSendFeedbackExceptionthrownbymockswhenexpectationsarenotmet,themockisnotproperlysetup,etc.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
[SerializableAttribute]
publicclassMockException:Exception
RemarksAdistinctexceptiontypeisprovidedsothatexceptionsthrownbythemockcanbedifferentiatedinteststhatexpectotherexceptionstobethrown(i.e.ArgumentException).
Richerexceptionhierarchy/typesarenotprovidedasteststypicallyshouldnotcatchorexpectexceptionsfromthemocks.Thesearetypicallytheresultofchangesinthetestedclassoritscollaboratorsimplementation,andresultinfixesinthemocksetupsothattheydissapearandallowthetesttopass.
InheritanceHierarchyObjectExceptionMoq..::.MockException
SeeAlsoMockExceptionMembersMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockExceptionMembersMockExceptionClassConstructorsMethodsSeeAlsoSendFeedback
TheMockExceptiontypeexposesthefollowingmembers.
Constructors Name Description
MockException Supportstheserializationinfrastructure.
Methods Name Description
GetObjectDataSupportstheserializationinfrastructure.(OverridesExceptionGetObjectData(SerializationInfo,StreamingContext).)
SeeAlsoMockExceptionClassMoqNamespace
C#
MoqMockExceptionConstructorMockExceptionClassSeeAlsoSendFeedbackSupportstheserializationinfrastructure.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
protectedMockException(
SerializationInfoinfo,
StreamingContextcontext
)
Parameters
infoType:SerializationInfoSerializationinformation.
contextType:StreamingContextStreamingcontext.
SeeAlsoMockExceptionClassMoqNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockExceptionMethodsMockExceptionClassSeeAlsoSendFeedback
TheMockExceptiontypeexposesthefollowingmembers.
Methods Name Description
GetObjectDataSupportstheserializationinfrastructure.(OverridesExceptionGetObjectData(SerializationInfo,StreamingContext).)
SeeAlsoMockExceptionClassMoqNamespace
C#
MoqMockException..::.GetObjectDataMethodMockExceptionClassSeeAlsoSendFeedbackSupportstheserializationinfrastructure.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicoverridevoidGetObjectData(
SerializationInfoinfo,
StreamingContextcontext
)
Parameters
infoType:SerializationInfoSerializationinformation.
contextType:StreamingContextStreamingcontext.
Implements
ISerializableGetObjectData(SerializationInfo,StreamingContext)_ExceptionGetObjectData(SerializationInfo,StreamingContext)
SeeAlsoMockExceptionClassMoqNamespace
C#
MoqMockFactoryClassMembersExampleSeeAlsoSendFeedbackUtilityfactoryclasstousetoconstructmultiplemockswhenconsistentverificationisdesiredforallofthem.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicclassMockFactory
RemarksIfmultiplemockswillbecreatedduringatest,passingthedesiredMockBehavior(ifdifferentthantheDefaultortheonepassedtothefactoryconstructor)andlaterverifyingeachmockcanbecomerepetitiveandtedious.
ThisfactoryclasshelpsinthatscenariobyprovidingasimplifiedcreationofmultiplemockswithadefaultMockBehavior(unlessoverridenbycallingCreate<(Of<(T>)>)(MockBehavior))andposteriorverification.
Copy
Copy
ExamplesThefollowingisastraightforwardexampleonhowtocreateandautomaticallyverifystrictmocksusingaMockFactory:
C#
varfactory=newMockFactory(MockBehavior.Strict);
varfoo=factory.Create<IFoo>();
varbar=factory.Create<IBar>();
//noneedtocallVerifiable()ontheexpectation
//aswe'llbevalidatingallexpectationsanyway.
foo.Expect(f=>f.Do());
bar.Expect(b=>b.Redo());
//exercisethemockshere
factory.VerifyAll();
//Atthispointallexpectationsarealreadychecked
//andanoptionalMockExceptionmightbethrown.
//Notealsothatbecausethemocksarestrict,anyinvocation
//thatdoesn'thaveamatchingexpectationwillalsothrowaMockException.
Thefollowingexamplesshowshowtosetupthefactorytocreateloosemocksandlaterverifyonlyverifiableexpectations:
C#
varfactory=newMockFactory(MockBehavior.Loose);
varfoo=factory.Create<IFoo>();
varbar=factory.Create<IBar>();
//thisexpectationwillbeverifiedattheendofthe"using"block
foo.Expect(f=>f.Do()).Verifiable();
//thisexpectationwillNOTbeverified
foo.Expect(f=>f.Calculate());
//thisexpectationwillbeverifiedattheendofthe"using"block
bar.Expect(b=>b.Redo()).Verifiable();
Copy
//exercisethemockshere
//notethatbecausethemocksareLoose,members
//calledintheinterfacesforwhichnomatching
//expectationsexistwillNOTthrowexceptions,
//andwillratherreturndefaultvalues.
factory.Verify();
//Atthispointverifiableexpectationsarealreadychecked
//andanoptionalMockExceptionmightbethrown.
Thefollowingexamplesshowshowtosetupthefactorywithadefaultstrictbehavior,overridingthatdefaultforaspecificmock:
C#
varfactory=newMockFactory(MockBehavior.Strict);
//thisparticularonewewantloose
varfoo=factory.Create<IFoo>(MockBehavior.Loose);
varbar=factory.Create<IBar>();
//setexpectations
//exercisethemockshere
factory.Verify();
InheritanceHierarchyObjectMoq..::.MockFactory
SeeAlsoMockFactoryMembersMoqNamespaceMoq..::.MockBehavior
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockFactoryMembersMockFactoryClassConstructorsMethodsPropertiesSeeAlsoSendFeedback
TheMockFactorytypeexposesthefollowingmembers.
Constructors Name Description
MockFactory InitializesthefactorywiththegivendefaultBehaviorfornewlycreatedmocksfromthefactory.
Methods Name Description
Create Overloaded.CreateMock<(Of<(T>)>)
Implementscreationofanewmockwithinthefactory.
Verify Verifiesallverifiableexpectationsonallmockscreatedbythisfactory.
VerifyAll Verifiesallverifiableexpectationsonallmockscreatedbythisfactory.
VerifyMocksInvokesverifyActionforeachmockinMocks,andaccumulatestheresultingMockVerificationExceptionthatmightbethrownfromtheaction.
Properties Name Description
CallBaseWhetherthebasemembervirtualimplementationwillbecalledformockedclassesifnoexpectationismet.DefaultstotrueTruetruetrue(TrueinVisualBasic).
DefaultValue Specifiesthebehaviortousewhenreturningdefaultvaluesforunexpectedinvocationsonloosemocks.
Mocks Getsthemocksthathavebeencreatedbythisfactoryandthatwillgetverifiedtogether.
C#
MoqMockFactoryConstructorMockFactoryClassSeeAlsoSendFeedbackInitializesthefactorywiththegivendefaultBehaviorfornewlycreatedmocksfromthefactory.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMockFactory(
MockBehaviordefaultBehavior
)
Parameters
defaultBehaviorType:Moq..::.MockBehaviorThebehaviortouseformockscreatedusingtheCreate<(Of<(T>)>)()()()factorymethodifnotoverridenbyusingtheCreate<(Of<(T>)>)(MockBehavior)overload.
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockFactoryMethodsMockFactoryClassSeeAlsoSendFeedback
TheMockFactorytypeexposesthefollowingmembers.
Methods Name Description
Create Overloaded.CreateMock<(Of<(T>)>)
Implementscreationofanewmockwithinthefactory.
Verify Verifiesallverifiableexpectationsonallmockscreatedbythisfactory.
VerifyAll Verifiesallverifiableexpectationsonallmockscreatedbythisfactory.
VerifyMocksInvokesverifyActionforeachmockinMocks,andaccumulatestheresultingMockVerificationExceptionthatmightbethrownfromtheaction.
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockFactory..::.CreateMethodMockFactoryClassSeeAlsoSendFeedback
OverloadList Name Description
Create<(Of<(T>)>)()()()CreatesanewmockwiththedefaultMockBehaviorspecifiedatfactoryconstructiontime.
Create<(Of<(T>)>)(MockBehavior)
Createsanewmockwiththegivenbehavior.
Create<(Of<(T>)>)(array<Object>[]()[])
CreatesanewmockwiththedefaultMockBehaviorspecifiedatfactoryconstructiontimeandwiththethegivenconstructorargumentsfortheclass.
Create<(Of<(T>)>)(MockBehavior,array<Object>[]()[])
Createsanewmockwiththegivenbehaviorandwiththethegivenconstructorargumentsfortheclass.
SeeAlsoMockFactoryClassMockFactoryMembersMoqNamespace
C#
MoqMockFactory..::.Create<(Of<(T>)>)MethodMockFactoryClassExampleSeeAlsoSendFeedbackCreatesanewmockwiththedefaultMockBehaviorspecifiedatfactoryconstructiontime.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMock<T>Create<T>()
whereT:class
TypeParametersT
Typetomock.
ReturnValue
AnewMock<(Of<(T>)>).
Copy
ExamplesC#
varfactory=newMockFactory(MockBehavior.Strict);
varfoo=factory.Create<IFoo>();
//usemockontests
factory.VerifyAll();
SeeAlsoMockFactoryClassCreateOverloadMoqNamespace
C#
MoqMockFactory..::.Create<(Of<(T>)>)Method(MockBehavior)MockFactoryClassExampleSeeAlsoSendFeedbackCreatesanewmockwiththegivenbehavior.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMock<T>Create<T>(
MockBehaviorbehavior
)
whereT:class
Parameters
behaviorType:Moq..::.MockBehaviorBehaviortouseforthemock,whichoverridesthedefaultbehaviorspecifiedatfactoryconstructiontime.
TypeParametersT
Typetomock.
ReturnValue
AnewMock<(Of<(T>)>).
Copy
ExamplesThefollowingexampleshowshowtocreateamockwithadifferentbehaviortothatspecifiedasthedefaultforthefactory:
C#
varfactory=newMockFactory(MockBehavior.Strict);
varfoo=factory.Create<IFoo>(MockBehavior.Loose);
SeeAlsoMockFactoryClassCreateOverloadMoqNamespace
C#
MoqMockFactory..::.Create<(Of<(T>)>)Method(array<>[]()[])MockFactoryClassExampleSeeAlsoSendFeedbackCreatesanewmockwiththedefaultMockBehaviorspecifiedatfactoryconstructiontimeandwiththethegivenconstructorargumentsfortheclass.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMock<T>Create<T>(
paramsObject[]args
)
whereT:class
Parameters
argsType:array<Object>[]()[]Constructorargumentsformockedclasses.
TypeParametersT
Typetomock.
ReturnValue
AnewMock<(Of<(T>)>).
RemarksThemockwilltrytofindthebestmatchconstructorgiventheconstructorarguments,andinvokethattoinitializetheinstance.Thisappliesonlytoclasses,notinterfaces.
Copy
ExamplesC#
varfactory=newMockFactory(MockBehavior.Default);
varmock=factory.Create<MyBase>("Foo",25,true);
//usemockontests
factory.Verify();
SeeAlsoMockFactoryClassCreateOverloadMoqNamespace
C#
MoqMockFactory..::.Create<(Of<(T>)>)Method(MockBehavior,array<>[]()[])MockFactoryClassExampleSeeAlsoSendFeedbackCreatesanewmockwiththegivenbehaviorandwiththethegivenconstructorargumentsfortheclass.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicMock<T>Create<T>(
MockBehaviorbehavior,
paramsObject[]args
)
whereT:class
Parameters
behaviorType:Moq..::.MockBehaviorBehaviortouseforthemock,whichoverridesthedefaultbehaviorspecifiedatfactoryconstructiontime.
argsType:array<Object>[]()[]Constructorargumentsformockedclasses.
TypeParametersT
Typetomock.
ReturnValue
AnewMock<(Of<(T>)>).
RemarksThemockwilltrytofindthebestmatchconstructorgiventheconstructorarguments,andinvokethattoinitializetheinstance.Thisappliesonlytoclasses,notinterfaces.
Copy
ExamplesThefollowingexampleshowshowtocreateamockwithadifferentbehaviortothatspecifiedasthedefaultforthefactory,passingconstructorarguments:
C#
varfactory=newMockFactory(MockBehavior.Default);
varmock=factory.Create<MyBase>(MockBehavior.Strict,"Foo",25,true
SeeAlsoMockFactoryClassCreateOverloadMoqNamespace
C#
MoqMockFactory..::.CreateMock<(Of<(T>)>)MethodMockFactoryClassSeeAlsoSendFeedbackImplementscreationofanewmockwithinthefactory.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
protectedvirtualMock<T>CreateMock<T>(
MockBehaviorbehavior,
Object[]args
)
whereT:class
Parameters
behaviorType:Moq..::.MockBehaviorThebehaviorforthenewmock.
argsType:array<Object>[]()[]Optionalargumentsfortheconstructionofthemock.
TypeParametersT
Typetomock.
C#
MoqMockFactory..::.VerifyMethodMockFactoryClassSeeAlsoSendFeedbackVerifiesallverifiableexpectationsonallmockscreatedbythisfactory.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicvirtualvoidVerify()
ExceptionsException Condition
Moq..::.MockException Oneormoremockshadexpectationsthatwerenotsatisfied.
SeeAlsoMockFactoryClassMoqNamespaceMock<(Of<(T>)>)..::.Verify()()()
C#
MoqMockFactory..::.VerifyAllMethodMockFactoryClassSeeAlsoSendFeedbackVerifiesallverifiableexpectationsonallmockscreatedbythisfactory.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicvirtualvoidVerifyAll()
ExceptionsException Condition
Moq..::.MockException Oneormoremockshadexpectationsthatwerenotsatisfied.
SeeAlsoMockFactoryClassMoqNamespaceMock<(Of<(T>)>)..::.Verify()()()
C#
MoqMockFactory..::.VerifyMocksMethodMockFactoryClassSeeAlsoSendFeedbackInvokesverifyActionforeachmockinMocks,andaccumulatestheresultingMockVerificationExceptionthatmightbethrownfromtheaction.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
protectedvirtualvoidVerifyMocks(
Action<IMock>verifyAction
)
Parameters
verifyActionType:Action<(Of<(IMock>)>)Theactiontoexecuteagainsteachmock.
C#IncludeProtectedMembersIncludeInheritedMembers
MoqMockFactoryPropertiesMockFactoryClassSeeAlsoSendFeedback
TheMockFactorytypeexposesthefollowingmembers.
Properties Name Description
CallBaseWhetherthebasemembervirtualimplementationwillbecalledformockedclassesifnoexpectationismet.DefaultstotrueTruetruetrue(TrueinVisualBasic).
DefaultValue Specifiesthebehaviortousewhenreturningdefaultvaluesforunexpectedinvocationsonloosemocks.
Mocks Getsthemocksthathavebeencreatedbythisfactoryandthatwillgetverifiedtogether.
C#
MoqMockFactory..::.CallBasePropertyMockFactoryClassSeeAlsoSendFeedbackWhetherthebasemembervirtualimplementationwillbecalledformockedclassesifnoexpectationismet.DefaultstotrueTruetruetrue(TrueinVisualBasic).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicboolCallBase{get;set;}
C#
MoqMockFactory..::.DefaultValuePropertyMockFactoryClassSeeAlsoSendFeedbackSpecifiesthebehaviortousewhenreturningdefaultvaluesforunexpectedinvocationsonloosemocks.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicDefaultValueDefaultValue{get;set;}
C#
MoqMockFactory..::.MocksPropertyMockFactoryClassSeeAlsoSendFeedbackGetsthemocksthathavebeencreatedbythisfactoryandthatwillgetverifiedtogether.
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
protectedIEnumerable<IMock>Mocks{get;}
C#
MoqRangeEnumerationSeeAlsoSendFeedbackKindofrangetouseinafilterspecifiedthroughIsInRange<(Of<(TValue>)>)(TValue,TValue,Range).
Namespace:MoqAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicenumRange
MembersMembername DescriptionInclusive Therangeincludesthetoandfromvalues.Exclusive Therangedoesnotincludethetoandfromvalues.
C#
MoqMoq.LanguageNamespaceSendFeedbackThisnamespacedefinestheinterfacesthatcontainthemethodsavailableinMoqfluentAPI,suchasExpect,Callback,Returns,Throws,etc.
Reviewthedocumentationoftheavailablemethodsontheseinterfacesforcodeexamples.Thewaytheseinterfacesarecomposed,groupedandmadevisibleatdifferentstagesduringanexpectation(i.e.Verifiableisthelast"verb"andcan'tbespecifiedbeforetheReturns)isinternalbuttheAPIwillnaturallyleadyoutotheproperwayofusingit,sodon'tworrytoomuchaboutwho(andwhere)exposestheselanguageinterfaces.TheywillshowupinIntellisensewhenit'sappropriate.
Dousetheirdocumentationtolearnaboutoptions(especiallywhenthereareseveraloverloadsavailable)thatmaybeabetterfitforaparticularscenario.
Interfaces Interface Description
ICallback DefinestheCallbackverbandoverloads.ICallback<(Of<(TResult>)>)
DefinestheCallbackverbandoverloadsforcallbacksonexpectationsthatreturnavalue.
ICallbackGetter<(Of<(TProperty>)>)
DefinestheCallbackverbforpropertygetterexpectations.
ICallbackSetter<(Of<(TProperty>)>)
DefinestheCallbackverbforpropertysetterexpectations.
INever DefinestheNeververb.
IOccurrence Definesoccurrencememberstoconstraintexpectations.
IRaise DefinestheRaisesverb.IReturns BaseinterfaceforIReturns<(Of<(TResult>)>).IReturns<(Of<(TResult>)>) DefinestheReturnsverb.
IReturnsGetter<(Of<(TProperty>)>)
DefinestheReturnsverbforpropertygetexpectations.
IThrows DefinestheThrowsverb.IVerifies DefinestheVerifiableverb.
C#
MoqICallbackInterfaceMembersSeeAlsoSendFeedbackDefinestheCallbackverbandoverloads.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceICallback
SeeAlsoICallbackMembersMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqICallbackMembersICallbackInterfaceMethodsSeeAlsoSendFeedback
Methods Name DescriptionCallback Overloaded.
SeeAlsoICallbackInterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqICallbackMethodsICallbackInterfaceSeeAlsoSendFeedback
Methods Name DescriptionCallback Overloaded.
SeeAlsoICallbackInterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqICallback..::.CallbackMethodICallbackInterfaceSeeAlsoSendFeedback
OverloadList Name Description
Callback(Action) Specifiesacallbacktoinvokewhenthemethodiscalled.
Callback<(Of<(T>)>)(Action<(Of<(T>)>))
Specifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Callback<(Of<(T1,T2>)>)(Action<(Of<(T1,T2>)>))
Specifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Callback<(Of<(T1,T2,T3>)>)(Action<(Of<(T1,T2,T3>)>))
Specifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Callback<(Of<(T1,T2,T3,T4>)>)(Action<(Of<(T1,T2,T3,T4>)>))
Specifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
SeeAlsoICallbackInterfaceICallbackMembersMoq.LanguageNamespace
C#
MoqICallback..::.CallbackMethod()ICallbackInterfaceExampleSeeAlsoSendFeedbackSpecifiesacallbacktoinvokewhenthemethodiscalled.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
ICallbackResultCallback(
Actioncallback
)
Parameters
callbackType:ActionCallbackmethodtoinvoke.
Copy
ExamplesThefollowingexamplespecifiesacallbacktosetabooleanvaluethatcanbeusedlater:
C#
boolcalled=false;
mock.Expect(x=>x.Execute())
.Callback(()=>called=true);
SeeAlsoICallbackInterfaceCallbackOverloadMoq.LanguageNamespace
C#
MoqICallback..::.Callback<(Of<(T>)>)Method(<(Of<(T>)>))ICallbackInterfaceExampleSeeAlsoSendFeedbackSpecifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
ICallbackResultCallback<T>(
Action<T>callback
)
Parameters
callbackType:Action<(Of<(T>)>)Callbackmethodtoinvoke.
TypeParametersT
Argumenttypeoftheinvokedmethod.
Copy
ExamplesInvokesthegivencallbackwiththeconcreteinvocationargumentvalue.
Noticehowthespecificstringargumentisretrievedbysimplydeclaringitaspartofthelambdaexpressionforthecallback:
C#
mock.Expect(x=>x.Execute(It.IsAny<string>()))
.Callback((stringcommand)=>Console.WriteLine(command));
SeeAlsoICallbackInterfaceCallbackOverloadMoq.LanguageNamespace
C#
MoqICallback..::.Callback<(Of<(T1,T2>)>)Method(<(Of<(T1,T2>)>))ICallbackInterfaceExampleSeeAlsoSendFeedbackSpecifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
ICallbackResultCallback<T1,T2>(
Action<T1,T2>callback
)
Parameters
callbackType:Action<(Of<(T1,T2>)>)Callbackmethodtoinvoke.
TypeParametersT1
Typeofthefirstargumentoftheinvokedmethod.T2
Typeofthesecondargumentoftheinvokedmethod.
Copy
ExamplesInvokesthegivencallbackwiththeconcreteinvocationargumentsvalues.
Noticehowthespecificargumentsareretrievedbysimplydeclaringthemaspartofthelambdaexpressionforthecallback:
C#
mock.Expect(x=>x.Execute(
It.IsAny<string>(),
It.IsAny<string>()))
.Callback((stringarg1,stringarg2)=>Console.WriteLine(arg1+arg2));
SeeAlsoICallbackInterfaceCallbackOverloadMoq.LanguageNamespace
C#
MoqICallback..::.Callback<(Of<(T1,T2,T3>)>)Method(<(Of<(T1,T2,T3>)>))ICallbackInterfaceExampleSeeAlsoSendFeedbackSpecifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
ICallbackResultCallback<T1,T2,T3>(
Action<T1,T2,T3>callback
)
Parameters
callbackType:Action<(Of<(T1,T2,T3>)>)Callbackmethodtoinvoke.
TypeParametersT1
Typeofthefirstargumentoftheinvokedmethod.T2
Typeofthesecondargumentoftheinvokedmethod.T3
Typeofthethirdargumentoftheinvokedmethod.
Copy
ExamplesInvokesthegivencallbackwiththeconcreteinvocationargumentsvalues.
Noticehowthespecificargumentsareretrievedbysimplydeclaringthemaspartofthelambdaexpressionforthecallback:
C#
mock.Expect(x=>x.Execute(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int>()))
.Callback((stringarg1,stringarg2,intarg3)=>Console.WriteLine(arg1+arg2+arg3));
SeeAlsoICallbackInterfaceCallbackOverloadMoq.LanguageNamespace
C#
MoqICallback..::.Callback<(Of<(T1,T2,T3,T4>)>)Method(<(Of<(T1,T2,T3,T4>)>))ICallbackInterfaceExampleSeeAlsoSendFeedbackSpecifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
ICallbackResultCallback<T1,T2,T3,T4>(
Action<T1,T2,T3,T4>callback
)
Parameters
callbackType:Action<(Of<(T1,T2,T3,T4>)>)Callbackmethodtoinvoke.
TypeParametersT1
Typeofthefirstargumentoftheinvokedmethod.T2
Typeofthesecondargumentoftheinvokedmethod.T3
Typeofthethirdargumentoftheinvokedmethod.T4
Typeofthefourthargumentoftheinvokedmethod.
Copy
ExamplesInvokesthegivencallbackwiththeconcreteinvocationargumentsvalues.
Noticehowthespecificargumentsareretrievedbysimplydeclaringthemaspartofthelambdaexpressionforthecallback:
C#
mock.Expect(x=>x.Execute(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<bool>()))
.Callback((stringarg1,stringarg2,intarg3,boolarg4)=>Console.WriteLine(arg1+arg2+arg3+arg4));
SeeAlsoICallbackInterfaceCallbackOverloadMoq.LanguageNamespace
C#
MoqICallback<(Of<(TResult>)>)InterfaceMembersSeeAlsoSendFeedbackDefinestheCallbackverbandoverloadsforcallbacksonexpectationsthatreturnavalue.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceICallback<TResult>
TypeParametersTResult
Typeofthereturnvalueoftheexpectation.
SeeAlsoICallback<(Of<(TResult>)>)MembersMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqICallback<(Of<(TResult>)>)MembersICallback<(Of<(TResult>)>)InterfaceMethodsSeeAlsoSendFeedback
Methods Name DescriptionCallback Overloaded.
SeeAlsoICallback<(Of<(TResult>)>)InterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqICallback<(Of<(TResult>)>)MethodsICallback<(Of<(TResult>)>)InterfaceSeeAlsoSendFeedback
Methods Name DescriptionCallback Overloaded.
SeeAlsoICallback<(Of<(TResult>)>)InterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqICallback<(Of<(TResult>)>)..::.CallbackMethodICallback<(Of<(TResult>)>)InterfaceSeeAlsoSendFeedback
OverloadList Name Description
Callback(Action) Specifiesacallbacktoinvokewhenthemethodiscalled.
Callback<(Of<(T>)>)(Action<(Of<(T>)>))
Specifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Callback<(Of<(T1,T2>)>)(Action<(Of<(T1,T2>)>))
Specifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Callback<(Of<(T1,T2,T3>)>)(Action<(Of<(T1,T2,T3>)>))
Specifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Callback<(Of<(T1,T2,T3,T4>)>)(Action<(Of<(T1,T2,T3,T4>)>))
Specifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
SeeAlsoICallback<(Of<(TResult>)>)InterfaceICallback<(Of<(TResult>)>)MembersMoq.LanguageNamespace
C#
MoqICallback<(Of<(TResult>)>)..::.CallbackMethod()ICallback<(Of<(TResult>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesacallbacktoinvokewhenthemethodiscalled.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsThrows<TResult>Callback(
Actioncallback
)
Parameters
callbackType:ActionCallbackmethodtoinvoke.
Copy
ExamplesThefollowingexamplespecifiesacallbacktosetabooleanvaluethatcanbeusedlater:
C#
boolcalled=false;
mock.Expect(x=>x.Execute())
.Callback(()=>called=true)
.Returns(true);
Notethatinthecaseofvalue-returningmethods,aftertheCallbackcallyoucanstillspecifythereturnvalue.
SeeAlsoICallback<(Of<(TResult>)>)InterfaceCallbackOverloadMoq.LanguageNamespace
C#
MoqICallback<(Of<(TResult>)>)..::.Callback<(Of<(T>)>)Method(<(Of<(T>)>))ICallback<(Of<(TResult>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsThrows<TResult>Callback<T>(
Action<T>callback
)
Parameters
callbackType:Action<(Of<(T>)>)Callbackmethodtoinvoke.
TypeParametersT
Typeoftheargumentoftheinvokedmethod.
Copy
ExamplesInvokesthegivencallbackwiththeconcreteinvocationargumentvalue.
Noticehowthespecificstringargumentisretrievedbysimplydeclaringitaspartofthelambdaexpressionforthecallback:
C#
mock.Expect(x=>x.Execute(It.IsAny<string>()))
.Callback((stringcommand)=>Console.WriteLine(command))
.Returns(true);
SeeAlsoICallback<(Of<(TResult>)>)InterfaceCallbackOverloadMoq.LanguageNamespace
C#
MoqICallback<(Of<(TResult>)>)..::.Callback<(Of<(T1,T2>)>)Method(<(Of<(T1,T2>)>))ICallback<(Of<(TResult>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsThrows<TResult>Callback<T1,T2>(
Action<T1,T2>callback
)
Parameters
callbackType:Action<(Of<(T1,T2>)>)Callbackmethodtoinvoke.
TypeParametersT1
Typeofthefirstargumentoftheinvokedmethod.T2
Typeofthesecondargumentoftheinvokedmethod.
Copy
ExamplesInvokesthegivencallbackwiththeconcreteinvocationargumentsvalues.
Noticehowthespecificargumentsareretrievedbysimplydeclaringthemaspartofthelambdaexpressionforthecallback:
C#
mock.Expect(x=>x.Execute(
It.IsAny<string>(),
It.IsAny<string>()))
.Callback((stringarg1,stringarg2)=>Console.WriteLine(arg1+arg2))
.Returns(true);
SeeAlsoICallback<(Of<(TResult>)>)InterfaceCallbackOverloadMoq.LanguageNamespace
C#
MoqICallback<(Of<(TResult>)>)..::.Callback<(Of<(T1,T2,T3>)>)Method(<(Of<(T1,T2,T3>)>))ICallback<(Of<(TResult>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsThrows<TResult>Callback<T1,T2,T3>(
Action<T1,T2,T3>callback
)
Parameters
callbackType:Action<(Of<(T1,T2,T3>)>)Callbackmethodtoinvoke.
TypeParametersT1
Typeofthefirstargumentoftheinvokedmethod.T2
Typeofthesecondargumentoftheinvokedmethod.T3
Typeofthethirdargumentoftheinvokedmethod.
Copy
ExamplesInvokesthegivencallbackwiththeconcreteinvocationargumentsvalues.
Noticehowthespecificargumentsareretrievedbysimplydeclaringthemaspartofthelambdaexpressionforthecallback:
C#
mock.Expect(x=>x.Execute(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int>()))
.Callback((stringarg1,stringarg2,intarg3)=>Console.WriteLine(arg1+arg2+arg3))
.Returns(true);
SeeAlsoICallback<(Of<(TResult>)>)InterfaceCallbackOverloadMoq.LanguageNamespace
C#
MoqICallback<(Of<(TResult>)>)..::.Callback<(Of<(T1,T2,T3,T4>)>)Method(<(Of<(T1,T2,T3,T4>)>))ICallback<(Of<(TResult>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesacallbacktoinvokewhenthemethodiscalledthatreceivestheoriginalarguments.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsThrows<TResult>Callback<T1,T2,T3,T4>(
Action<T1,T2,T3,T4>callback
)
Parameters
callbackType:Action<(Of<(T1,T2,T3,T4>)>)Callbackmethodtoinvoke.
TypeParametersT1
Typeofthefirstargumentoftheinvokedmethod.T2
Typeofthesecondargumentoftheinvokedmethod.T3
Typeofthethirdargumentoftheinvokedmethod.T4
Typeofthefourthargumentoftheinvokedmethod.
Copy
ExamplesInvokesthegivencallbackwiththeconcreteinvocationargumentsvalues.
Noticehowthespecificargumentsareretrievedbysimplydeclaringthemaspartofthelambdaexpressionforthecallback:
C#
mock.Expect(x=>x.Execute(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<bool>()))
.Callback((stringarg1,stringarg2,intarg3,boolarg4)=>Console.WriteLine(arg1+arg2+arg3+arg4))
.Returns(true);
SeeAlsoICallback<(Of<(TResult>)>)InterfaceCallbackOverloadMoq.LanguageNamespace
C#
MoqICallbackGetter<(Of<(TProperty>)>)InterfaceMembersSeeAlsoSendFeedbackDefinestheCallbackverbforpropertygetterexpectations.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceICallbackGetter<TProperty>
TypeParametersTProperty
Typeoftheproperty.
SeeAlsoICallbackGetter<(Of<(TProperty>)>)MembersMoq.LanguageNamespaceMock<(Of<(T>)>)..::.ExpectGet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>))
C#IncludeProtectedMembersIncludeInheritedMembers
MoqICallbackGetter<(Of<(TProperty>)>)MembersICallbackGetter<(Of<(TProperty>)>)InterfaceMethodsSeeAlsoSendFeedback
TheICallbackGetter<(Of<(TProperty>)>)typeexposesthefollowingmembers.
Methods Name Description
Callback Specifiesacallbacktoinvokewhenthepropertyisretrieved.
SeeAlsoICallbackGetter<(Of<(TProperty>)>)InterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqICallbackGetter<(Of<(TProperty>)>)MethodsICallbackGetter<(Of<(TProperty>)>)InterfaceSeeAlsoSendFeedback
TheICallbackGetter<(Of<(TProperty>)>)typeexposesthefollowingmembers.
Methods Name Description
Callback Specifiesacallbacktoinvokewhenthepropertyisretrieved.
SeeAlsoICallbackGetter<(Of<(TProperty>)>)InterfaceMoq.LanguageNamespace
C#
MoqICallbackGetter<(Of<(TProperty>)>)..::.CallbackMethodICallbackGetter<(Of<(TProperty>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesacallbacktoinvokewhenthepropertyisretrieved.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsThrowsGetter<TProperty>Callback(
Actioncallback
)
Parameters
callbackType:ActionCallbackmethodtoinvoke.
Copy
ExamplesInvokesthegivencallbackwiththepropertyvaluebeingset.
C#
mock.ExpectGet(x=>x.Suspended)
.Callback(()=>called=true)
.Returns(true);
SeeAlsoICallbackGetter<(Of<(TProperty>)>)InterfaceMoq.LanguageNamespace
C#
MoqICallbackSetter<(Of<(TProperty>)>)InterfaceMembersSeeAlsoSendFeedbackDefinestheCallbackverbforpropertysetterexpectations.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceICallbackSetter<TProperty>
TypeParametersTProperty
Typeoftheproperty.
SeeAlsoICallbackSetter<(Of<(TProperty>)>)MembersMoq.LanguageNamespaceIMock<(Of<(T>)>)..::.ExpectSet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>))IMock<(Of<(T>)>)..::.ExpectSet<(Of<(TProperty>)>)(Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>),TProperty)
C#IncludeProtectedMembersIncludeInheritedMembers
MoqICallbackSetter<(Of<(TProperty>)>)MembersICallbackSetter<(Of<(TProperty>)>)InterfaceMethodsSeeAlsoSendFeedback
TheICallbackSetter<(Of<(TProperty>)>)typeexposesthefollowingmembers.
Methods Name Description
Callback Specifiesacallbacktoinvokewhenthepropertyissetthatreceivesthepropertyvaluebeingset.
SeeAlsoICallbackSetter<(Of<(TProperty>)>)InterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqICallbackSetter<(Of<(TProperty>)>)MethodsICallbackSetter<(Of<(TProperty>)>)InterfaceSeeAlsoSendFeedback
TheICallbackSetter<(Of<(TProperty>)>)typeexposesthefollowingmembers.
Methods Name Description
Callback Specifiesacallbacktoinvokewhenthepropertyissetthatreceivesthepropertyvaluebeingset.
SeeAlsoICallbackSetter<(Of<(TProperty>)>)InterfaceMoq.LanguageNamespace
C#
MoqICallbackSetter<(Of<(TProperty>)>)..::.CallbackMethodICallbackSetter<(Of<(TProperty>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesacallbacktoinvokewhenthepropertyissetthatreceivesthepropertyvaluebeingset.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
ICallbackResultCallback(
Action<TProperty>callback
)
Parameters
callbackType:Action<(Of<(TProperty>)>)Callbackmethodtoinvoke.
Copy
ExamplesInvokesthegivencallbackwiththepropertyvaluebeingset.
C#
mock.ExpectSet(x=>x.Suspended)
.Callback((boolstate)=>Console.WriteLine(state));
SeeAlsoICallbackSetter<(Of<(TProperty>)>)InterfaceMoq.LanguageNamespace
C#
MoqINeverInterfaceMembersSeeAlsoSendFeedbackDefinestheNeververb.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceINever
SeeAlsoINeverMembersMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqINeverMembersINeverInterfaceMethodsSeeAlsoSendFeedback
TheINevertypeexposesthefollowingmembers.
Methods Name Description
Never Theexpectedinvocationisneverexpectedtohappen.
SeeAlsoINeverInterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqINeverMethodsINeverInterfaceSeeAlsoSendFeedback
TheINevertypeexposesthefollowingmembers.
Methods Name Description
Never Theexpectedinvocationisneverexpectedtohappen.
SeeAlsoINeverInterfaceMoq.LanguageNamespace
C#
MoqINever..::.NeverMethodINeverInterfaceExampleSeeAlsoSendFeedbackTheexpectedinvocationisneverexpectedtohappen.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
voidNever()
RemarksNever()()()isalwaysverifiedinmediatelyastheinvocationsareperformed,likestrictmocksdowithunexpectedinvocations.
Copy
ExamplesC#
varmock=newMock<ICommand>();
mock.Expect(foo=>foo.Execute("ping"))
.Never();
SeeAlsoINeverInterfaceMoq.LanguageNamespace
C#
MoqIOccurrenceInterfaceMembersSeeAlsoSendFeedbackDefinesoccurrencememberstoconstraintexpectations.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceIOccurrence
SeeAlsoIOccurrenceMembersMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIOccurrenceMembersIOccurrenceInterfaceMethodsSeeAlsoSendFeedback
TheIOccurrencetypeexposesthefollowingmembers.
Methods Name Description
AtMost Theexpectedinvocationcanhappenatmostspecifiednumberoftimes.
AtMostOnce Theexpectedinvocationcanhappenatmostonce.
SeeAlsoIOccurrenceInterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIOccurrenceMethodsIOccurrenceInterfaceSeeAlsoSendFeedback
TheIOccurrencetypeexposesthefollowingmembers.
Methods Name Description
AtMost Theexpectedinvocationcanhappenatmostspecifiednumberoftimes.
AtMostOnce Theexpectedinvocationcanhappenatmostonce.
SeeAlsoIOccurrenceInterfaceMoq.LanguageNamespace
C#
MoqIOccurrence..::.AtMostMethodIOccurrenceInterfaceExampleSeeAlsoSendFeedbackTheexpectedinvocationcanhappenatmostspecifiednumberoftimes.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IVerifiesAtMost(
intcallCount
)
Parameters
callCountType:Int32
[Missing<paramname="callCount"/>documentationfor"M:Moq.Language.IOccurrence.AtMost(System.Int32)"]
Copy
ExamplesC#
varmock=newMock<ICommand>();
mock.Expect(foo=>foo.Execute("ping"))
.AtMost(5);
SeeAlsoIOccurrenceInterfaceMoq.LanguageNamespace
C#
MoqIOccurrence..::.AtMostOnceMethodIOccurrenceInterfaceExampleSeeAlsoSendFeedbackTheexpectedinvocationcanhappenatmostonce.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IVerifiesAtMostOnce()
Copy
ExamplesC#
varmock=newMock<ICommand>();
mock.Expect(foo=>foo.Execute("ping"))
.AtMostOnce();
SeeAlsoIOccurrenceInterfaceMoq.LanguageNamespace
C#
MoqIRaiseInterfaceMembersSeeAlsoSendFeedbackDefinestheRaisesverb.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceIRaise
SeeAlsoIRaiseMembersMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIRaiseMembersIRaiseInterfaceMethodsSeeAlsoSendFeedback
Methods Name DescriptionRaises Overloaded.
SeeAlsoIRaiseInterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIRaiseMethodsIRaiseInterfaceSeeAlsoSendFeedback
Methods Name DescriptionRaises Overloaded.
SeeAlsoIRaiseInterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIRaise..::.RaisesMethodIRaiseInterfaceSeeAlsoSendFeedback
OverloadList Name Description
Raises(MockedEvent,EventArgs)Specifiesthemockedeventthatwillberaisedwhentheexpectationismet.
Raises(MockedEvent,Func<(Of<(EventArgs>)>))
Specifiesthemockedeventthatwillberaisedwhentheexpectationismet.
Raises<(Of<(T>)>)(MockedEvent,Func<(Of<(T,EventArgs>)>))
Specifiesthemockedeventthatwillberaisedwhentheexpectationismet.
Raises<(Of<(T1,T2>)>)(MockedEvent,Func<(Of<(T1,T2,EventArgs>)>))
Specifiesthemockedeventthatwillberaisedwhentheexpectationismet.
Raises<(Of<(T1,T2,T3>)>)(MockedEvent,Func<(Of<(T1,T2,T3,EventArgs>)>))
Specifiesthemockedeventthatwillberaisedwhentheexpectationismet.
Raises<(Of<(T1,T2,T3,T4>)>)(MockedEvent,Func<(Of<(T1,T2,T3,T4,EventArgs>)>))
Specifiesthemockedeventthatwillberaisedwhentheexpectationismet.
SeeAlsoIRaiseInterfaceIRaiseMembersMoq.LanguageNamespace
C#
MoqIRaise..::.RaisesMethod(MockedEvent,)IRaiseInterfaceExampleSeeAlsoSendFeedbackSpecifiesthemockedeventthatwillberaisedwhentheexpectationismet.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IVerifiesRaises(
MockedEventeventHandler,
EventArgsargs
)
Parameters
eventHandlerType:Moq..::.MockedEventThemockedevent,retrievedfromCreateEventHandler()()()orCreateEventHandler<(Of<(TEventArgs>)>)()()().
argsType:EventArgsTheeventargstopasswhenraisingtheevent.
Copy
ExamplesThefollowingexampleshowshowtosetanexpectationthatwillraiseaneventwhenit'smet:
C#
varmock=newMock<IContainer>();
//createhandlertoassociatewiththeeventtoraise
varhandler=mock.CreateEventHandler();
//associatethehandlerwiththeeventtoraise
mock.Object.Added+=handler;
//settheexpectationandthehandlertoraise
mock.Expect(add=>add.Add(It.IsAny<string>(),It.IsAny<object>()))
.Raises(handler,EventArgs.Empty);
SeeAlsoIRaiseInterfaceRaisesOverloadMoq.LanguageNamespace
C#
MoqIRaise..::.RaisesMethod(MockedEvent,<(Of<(>)>))IRaiseInterfaceSeeAlsoSendFeedbackSpecifiesthemockedeventthatwillberaisedwhentheexpectationismet.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IVerifiesRaises(
MockedEventeventHandler,
Func<EventArgs>func
)
Parameters
eventHandlerType:Moq..::.MockedEventThemockedevent,retrievedfromCreateEventHandler()()()orCreateEventHandler<(Of<(TEventArgs>)>)()()().
funcType:Func<(Of<(EventArgs>)>)AfunctionthatwillbuildtheEventArgstopasswhenraisingtheevent.
SeeAlsoIRaiseInterfaceRaisesOverloadMoq.LanguageNamespaceIRaise..::.Raises(MockedEvent,EventArgs)
C#
MoqIRaise..::.Raises<(Of<(T>)>)Method(MockedEvent,<(Of<(T,>)>))IRaiseInterfaceSeeAlsoSendFeedbackSpecifiesthemockedeventthatwillberaisedwhentheexpectationismet.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IVerifiesRaises<T>(
MockedEventeventHandler,
Func<T,EventArgs>func
)
Parameters
eventHandlerType:Moq..::.MockedEventThemockedevent,retrievedfromCreateEventHandler()()()orCreateEventHandler<(Of<(TEventArgs>)>)()()().
funcType:Func<(Of<(T,EventArgs>)>)AfunctionthatwillbuildtheEventArgstopasswhenraisingtheevent.
TypeParametersT
Typeoftheargumentreceivedbytheexpectedinvocation.
SeeAlsoIRaiseInterfaceRaisesOverloadMoq.LanguageNamespaceIRaise..::.Raises(MockedEvent,EventArgs)
C#
MoqIRaise..::.Raises<(Of<(T1,T2>)>)Method(MockedEvent,<(Of<(T1,T2,>)>))IRaiseInterfaceSeeAlsoSendFeedbackSpecifiesthemockedeventthatwillberaisedwhentheexpectationismet.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IVerifiesRaises<T1,T2>(
MockedEventeventHandler,
Func<T1,T2,EventArgs>func
)
Parameters
eventHandlerType:Moq..::.MockedEventThemockedevent,retrievedfromCreateEventHandler()()()orCreateEventHandler<(Of<(TEventArgs>)>)()()().
funcType:Func<(Of<(T1,T2,EventArgs>)>)AfunctionthatwillbuildtheEventArgstopasswhenraisingtheevent.
TypeParametersT1
Typeofthefirstargumentreceivedbytheexpectedinvocation.T2
Typeofthesecondargumentreceivedbytheexpectedinvocation.
SeeAlsoIRaiseInterfaceRaisesOverloadMoq.LanguageNamespaceIRaise..::.Raises(MockedEvent,EventArgs)
C#
MoqIRaise..::.Raises<(Of<(T1,T2,T3>)>)Method(MockedEvent,<(Of<(T1,T2,T3,>)>))IRaiseInterfaceSeeAlsoSendFeedbackSpecifiesthemockedeventthatwillberaisedwhentheexpectationismet.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IVerifiesRaises<T1,T2,T3>(
MockedEventeventHandler,
Func<T1,T2,T3,EventArgs>func
)
Parameters
eventHandlerType:Moq..::.MockedEventThemockedevent,retrievedfromCreateEventHandler()()()orCreateEventHandler<(Of<(TEventArgs>)>)()()().
funcType:Func<(Of<(T1,T2,T3,EventArgs>)>)AfunctionthatwillbuildtheEventArgstopasswhenraisingtheevent.
TypeParametersT1
Typeofthefirstargumentreceivedbytheexpectedinvocation.T2
Typeofthesecondargumentreceivedbytheexpectedinvocation.T3
Typeofthethirdargumentreceivedbytheexpectedinvocation.
SeeAlsoIRaiseInterfaceRaisesOverloadMoq.LanguageNamespaceIRaise..::.Raises(MockedEvent,EventArgs)
C#
MoqIRaise..::.Raises<(Of<(T1,T2,T3,T4>)>)Method(MockedEvent,<(Of<(T1,T2,T3,T4,>)>))IRaiseInterfaceSeeAlsoSendFeedbackSpecifiesthemockedeventthatwillberaisedwhentheexpectationismet.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IVerifiesRaises<T1,T2,T3,T4>(
MockedEventeventHandler,
Func<T1,T2,T3,T4,EventArgs>func
)
Parameters
eventHandlerType:Moq..::.MockedEventThemockedevent,retrievedfromCreateEventHandler()()()orCreateEventHandler<(Of<(TEventArgs>)>)()()().
funcType:Func<(Of<(T1,T2,T3,T4,EventArgs>)>)AfunctionthatwillbuildtheEventArgstopasswhenraisingtheevent.
TypeParametersT1
Typeofthefirstargumentreceivedbytheexpectedinvocation.T2
Typeofthesecondargumentreceivedbytheexpectedinvocation.T3
Typeofthethirdargumentreceivedbytheexpectedinvocation.T4
Typeofthefourthargumentreceivedbytheexpectedinvocation.
SeeAlsoIRaiseInterfaceRaisesOverloadMoq.LanguageNamespaceIRaise..::.Raises(MockedEvent,EventArgs)
C#
MoqIReturnsInterfaceSeeAlsoSendFeedbackBaseinterfaceforIReturns<(Of<(TResult>)>).
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceIReturns
C#
MoqIReturns<(Of<(TResult>)>)InterfaceMembersSeeAlsoSendFeedbackDefinestheReturnsverb.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceIReturns<TResult>:IReturns
TypeParametersTResult
Typeofthereturnvaluefromtheexpression.
SeeAlsoIReturns<(Of<(TResult>)>)MembersMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIReturns<(Of<(TResult>)>)MembersIReturns<(Of<(TResult>)>)InterfaceMethodsSeeAlsoSendFeedback
Methods Name DescriptionReturns Overloaded.
SeeAlsoIReturns<(Of<(TResult>)>)InterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIReturns<(Of<(TResult>)>)MethodsIReturns<(Of<(TResult>)>)InterfaceSeeAlsoSendFeedback
Methods Name DescriptionReturns Overloaded.
SeeAlsoIReturns<(Of<(TResult>)>)InterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIReturns<(Of<(TResult>)>)..::.ReturnsMethodIReturns<(Of<(TResult>)>)InterfaceSeeAlsoSendFeedback
OverloadList Name Description
Returns(Func<(Of<(TResult>)>))
Specifiesafunctionthatwillcalculatethevaluetoreturnfromthemethod.
Returns<(Of<(T>)>)(Func<(Of<(T,TResult>)>))
Specifiesafunctionthatwillcalculatethevaluetoreturnfromthemethod,retrievingtheargumentsfortheinvocation.
Returns<(Of<(T1,T2>)>)(Func<(Of<(T1,T2,TResult>)>))
Specifiesafunctionthatwillcalculatethevaluetoreturnfromthemethod,retrievingtheargumentsfortheinvocation.
Returns<(Of<(T1,T2,T3>)>)(Func<(Of<(T1,T2,T3,TResult>)>))
Specifiesafunctionthatwillcalculatethevaluetoreturnfromthemethod,retrievingtheargumentsfortheinvocation.
Returns<(Of<(T1,T2,T3,T4>)>)(Func<(Of<(T1,T2,T3,T4,TResult>)>))
Specifiesafunctionthatwillcalculatethevaluetoreturnfromthemethod,retrievingtheargumentsfortheinvocation.
Returns(TResult) Specifiesthevaluetoreturn.
SeeAlsoIReturns<(Of<(TResult>)>)InterfaceIReturns<(Of<(TResult>)>)MembersMoq.LanguageNamespace
C#
MoqIReturns<(Of<(TResult>)>)..::.ReturnsMethod(<(Of<(TResult>)>))IReturns<(Of<(TResult>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesafunctionthatwillcalculatethevaluetoreturnfromthemethod.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsResultReturns(
Func<TResult>valueFunction
)
Parameters
valueFunctionType:Func<(Of<(TResult>)>)Thefunctionthatwillcalculatethereturnvalue.
Copy
ExamplesReturnacalculatedvaluewhenthemethodiscalled:
C#
mock.Expect(x=>x.Execute("ping"))
.Returns(()=>returnValues[0]);
Thelambdaexpressiontoretrievethereturnvalueislazy-executed,meaningthatitsvaluemaychangedependingonthemomentthemethodisexecutedandthevaluethereturnValuesarrayhasatthatmoment.
SeeAlsoIReturns<(Of<(TResult>)>)InterfaceReturnsOverloadMoq.LanguageNamespace
C#
MoqIReturns<(Of<(TResult>)>)..::.Returns<(Of<(T>)>)Method(<(Of<(T,TResult>)>))IReturns<(Of<(TResult>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesafunctionthatwillcalculatethevaluetoreturnfromthemethod,retrievingtheargumentsfortheinvocation.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsResultReturns<T>(
Func<T,TResult>valueFunction
)
Parameters
valueFunctionType:Func<(Of<(T,TResult>)>)Thefunctionthatwillcalculatethereturnvalue.
TypeParametersT
Typeoftheargumentoftheinvokedmethod.
Copy
ExamplesReturnacalculatedvaluewhichisevaluatedlazilyatthetimeoftheinvocation.
Thelookuplistcanchangebetweeninvocationsandtheexpectationwillreturndifferentvaluesaccordingly.Also,noticehowthespecificstringargumentisretrievedbysimplydeclaringitaspartofthelambdaexpression:
C#
mock.Expect(x=>x.Execute(It.IsAny<string>()))
.Returns((stringcommand)=>returnValues[command]);
SeeAlsoIReturns<(Of<(TResult>)>)InterfaceReturnsOverloadMoq.LanguageNamespace
C#
MoqIReturns<(Of<(TResult>)>)..::.Returns<(Of<(T1,T2>)>)Method(<(Of<(T1,T2,TResult>)>))IReturns<(Of<(TResult>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesafunctionthatwillcalculatethevaluetoreturnfromthemethod,retrievingtheargumentsfortheinvocation.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsResultReturns<T1,T2>(
Func<T1,T2,TResult>valueFunction
)
Parameters
valueFunctionType:Func<(Of<(T1,T2,TResult>)>)Thefunctionthatwillcalculatethereturnvalue.
TypeParametersT1
Typeofthefirstargumentoftheinvokedmethod.T2
Typeofthesecondargumentoftheinvokedmethod.
Copy
ExamplesReturnacalculatedvaluewhichisevaluatedlazilyatthetimeoftheinvocation.
Thereturnvalueiscalculatedfromthevalueoftheactualmethodinvocationarguments.Noticehowtheargumentsareretrievedbysimplydeclaringthemaspartofthelambdaexpression:
C#
mock.Expect(x=>x.Execute(
It.IsAny<string>(),
It.IsAny<string>()))
.Returns((stringarg1,stringarg2)=>arg1+arg2);
SeeAlsoIReturns<(Of<(TResult>)>)InterfaceReturnsOverloadMoq.LanguageNamespace
C#
MoqIReturns<(Of<(TResult>)>)..::.Returns<(Of<(T1,T2,T3>)>)Method(<(Of<(T1,T2,T3,TResult>)>))IReturns<(Of<(TResult>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesafunctionthatwillcalculatethevaluetoreturnfromthemethod,retrievingtheargumentsfortheinvocation.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsResultReturns<T1,T2,T3>(
Func<T1,T2,T3,TResult>valueFunction
)
Parameters
valueFunctionType:Func<(Of<(T1,T2,T3,TResult>)>)Thefunctionthatwillcalculatethereturnvalue.
TypeParametersT1
Typeofthefirstargumentoftheinvokedmethod.T2
Typeofthesecondargumentoftheinvokedmethod.T3
Typeofthethirdargumentoftheinvokedmethod.
Copy
ExamplesReturnacalculatedvaluewhichisevaluatedlazilyatthetimeoftheinvocation.
Thereturnvalueiscalculatedfromthevalueoftheactualmethodinvocationarguments.Noticehowtheargumentsareretrievedbysimplydeclaringthemaspartofthelambdaexpression:
C#
mock.Expect(x=>x.Execute(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int>()))
.Returns((stringarg1,stringarg2,intarg3)=>arg1+arg2+arg3);
SeeAlsoIReturns<(Of<(TResult>)>)InterfaceReturnsOverloadMoq.LanguageNamespace
C#
MoqIReturns<(Of<(TResult>)>)..::.Returns<(Of<(T1,T2,T3,T4>)>)Method(<(Of<(T1,T2,T3,T4,TResult>)>))IReturns<(Of<(TResult>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesafunctionthatwillcalculatethevaluetoreturnfromthemethod,retrievingtheargumentsfortheinvocation.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsResultReturns<T1,T2,T3,T4>(
Func<T1,T2,T3,T4,TResult>valueFunction
)
Parameters
valueFunctionType:Func<(Of<(T1,T2,T3,T4,TResult>)>)Thefunctionthatwillcalculatethereturnvalue.
TypeParametersT1
Typeofthefirstargumentoftheinvokedmethod.T2
Typeofthesecondargumentoftheinvokedmethod.T3
Typeofthethirdargumentoftheinvokedmethod.T4
Typeofthefourthargumentoftheinvokedmethod.
Copy
ExamplesReturnacalculatedvaluewhichisevaluatedlazilyatthetimeoftheinvocation.
Thereturnvalueiscalculatedfromthevalueoftheactualmethodinvocationarguments.Noticehowtheargumentsareretrievedbysimplydeclaringthemaspartofthelambdaexpression:
C#
mock.Expect(x=>x.Execute(
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<bool>()))
.Returns((stringarg1,stringarg2,intarg3,boolarg4)=>arg1+arg2+arg3+arg4);
SeeAlsoIReturns<(Of<(TResult>)>)InterfaceReturnsOverloadMoq.LanguageNamespace
C#
MoqIReturns<(Of<(TResult>)>)..::.ReturnsMethod(TResult)IReturns<(Of<(TResult>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesthevaluetoreturn.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsResultReturns(
TResultvalue
)
Parameters
valueType:TResultThevaluetoreturn,ornullNothingnullptranullreference(NothinginVisualBasic).
Copy
ExamplesReturnatruevaluefromthemethodcall:
C#
mock.Expect(x=>x.Execute("ping"))
.Returns(true);
SeeAlsoIReturns<(Of<(TResult>)>)InterfaceReturnsOverloadMoq.LanguageNamespace
C#
MoqIReturnsGetter<(Of<(TProperty>)>)InterfaceMembersSeeAlsoSendFeedbackDefinestheReturnsverbforpropertygetexpectations.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceIReturnsGetter<TProperty>:IReturns
TypeParametersTProperty
Typeoftheproperty.
SeeAlsoIReturnsGetter<(Of<(TProperty>)>)MembersMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIReturnsGetter<(Of<(TProperty>)>)MembersIReturnsGetter<(Of<(TProperty>)>)InterfaceMethodsSeeAlsoSendFeedback
Methods Name DescriptionReturns Overloaded.
SeeAlsoIReturnsGetter<(Of<(TProperty>)>)InterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIReturnsGetter<(Of<(TProperty>)>)MethodsIReturnsGetter<(Of<(TProperty>)>)InterfaceSeeAlsoSendFeedback
Methods Name DescriptionReturns Overloaded.
SeeAlsoIReturnsGetter<(Of<(TProperty>)>)InterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIReturnsGetter<(Of<(TProperty>)>)..::.ReturnsMethodIReturnsGetter<(Of<(TProperty>)>)InterfaceSeeAlsoSendFeedback
OverloadList Name Description
Returns(Func<(Of<(TProperty>)>))
Specifiesafunctionthatwillcalculatethevaluetoreturnfortheproperty.
Returns(TProperty) Specifiesthevaluetoreturn.
SeeAlsoIReturnsGetter<(Of<(TProperty>)>)InterfaceIReturnsGetter<(Of<(TProperty>)>)MembersMoq.LanguageNamespace
C#
MoqIReturnsGetter<(Of<(TProperty>)>)..::.ReturnsMethod(<(Of<(TProperty>)>))IReturnsGetter<(Of<(TProperty>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesafunctionthatwillcalculatethevaluetoreturnfortheproperty.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsResultReturns(
Func<TProperty>valueFunction
)
Parameters
valueFunctionType:Func<(Of<(TProperty>)>)Thefunctionthatwillcalculatethereturnvalue.
Copy
ExamplesReturnacalculatedvaluewhenthepropertyisretrieved:
C#
mock.ExpectGet(x=>x.Suspended)
.Returns(()=>returnValues[0]);
Thelambdaexpressiontoretrievethereturnvalueislazy-executed,meaningthatitsvaluemaychangedependingonthemomentthepropertyisretrievedandthevaluethereturnValuesarrayhasatthatmoment.
SeeAlsoIReturnsGetter<(Of<(TProperty>)>)InterfaceReturnsOverloadMoq.LanguageNamespace
C#
MoqIReturnsGetter<(Of<(TProperty>)>)..::.ReturnsMethod(TProperty)IReturnsGetter<(Of<(TProperty>)>)InterfaceExampleSeeAlsoSendFeedbackSpecifiesthevaluetoreturn.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IReturnsResultReturns(
TPropertyvalue
)
Parameters
valueType:TPropertyThevaluetoreturn,ornullNothingnullptranullreference(NothinginVisualBasic).
Copy
ExamplesReturnatruevaluefromthepropertygettercall:
C#
mock.ExpectGet(x=>x.Suspended)
.Returns(true);
SeeAlsoIReturnsGetter<(Of<(TProperty>)>)InterfaceReturnsOverloadMoq.LanguageNamespace
C#
MoqIThrowsInterfaceMembersSeeAlsoSendFeedbackDefinestheThrowsverb.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceIThrows
SeeAlsoIThrowsMembersMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIThrowsMembersIThrowsInterfaceMethodsSeeAlsoSendFeedback
Methods Name DescriptionThrows Overloaded.
SeeAlsoIThrowsInterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIThrowsMethodsIThrowsInterfaceSeeAlsoSendFeedback
Methods Name DescriptionThrows Overloaded.
SeeAlsoIThrowsInterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIThrows..::.ThrowsMethodIThrowsInterfaceSeeAlsoSendFeedback
OverloadList Name Description
Throws<(Of<(TException>)>)()()()
Specifiesthetypeofexceptiontothrowwhenthemethodisinvoked.
Throws(Exception) Specifiestheexceptiontothrowwhenthemethodisinvoked.
SeeAlsoIThrowsInterfaceIThrowsMembersMoq.LanguageNamespace
C#
MoqIThrows..::.Throws<(Of<(TException>)>)MethodIThrowsInterfaceExampleSeeAlsoSendFeedbackSpecifiesthetypeofexceptiontothrowwhenthemethodisinvoked.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IThrowsResultThrows<TException>()
whereTException:new(),Exception
TypeParametersTException
Typeofexceptiontoinstantiateandthrowwhentheexpectationismet.
Copy
ExamplesThisexampleshowshowtothrowanexceptionwhenthemethodisinvokedwithanemptystringargument:
C#
mock.Expect(x=>x.Execute(""))
.Throws<ArgumentException>();
SeeAlsoIThrowsInterfaceThrowsOverloadMoq.LanguageNamespace
C#
MoqIThrows..::.ThrowsMethod()IThrowsInterfaceExampleSeeAlsoSendFeedbackSpecifiestheexceptiontothrowwhenthemethodisinvoked.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IThrowsResultThrows(
Exceptionexception
)
Parameters
exceptionType:ExceptionExceptioninstancetothrow.
Copy
ExamplesThisexampleshowshowtothrowanexceptionwhenthemethodisinvokedwithanemptystringargument:
C#
mock.Expect(x=>x.Execute(""))
.Throws(newArgumentException());
SeeAlsoIThrowsInterfaceThrowsOverloadMoq.LanguageNamespace
C#
MoqIVerifiesInterfaceMembersSeeAlsoSendFeedbackDefinestheVerifiableverb.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceIVerifies
SeeAlsoIVerifiesMembersMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIVerifiesMembersIVerifiesInterfaceMethodsSeeAlsoSendFeedback
TheIVerifiestypeexposesthefollowingmembers.
Methods Name Description
Verifiable Markstheexpectationasverifiable,meaningthatacalltoVerify()()()willcheckifthisparticularexpectationwasmet.
SeeAlsoIVerifiesInterfaceMoq.LanguageNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIVerifiesMethodsIVerifiesInterfaceSeeAlsoSendFeedback
TheIVerifiestypeexposesthefollowingmembers.
Methods Name Description
Verifiable Markstheexpectationasverifiable,meaningthatacalltoVerify()()()willcheckifthisparticularexpectationwasmet.
SeeAlsoIVerifiesInterfaceMoq.LanguageNamespace
C#
MoqIVerifies..::.VerifiableMethodIVerifiesInterfaceExampleSeeAlsoSendFeedbackMarkstheexpectationasverifiable,meaningthatacalltoVerify()()()willcheckifthisparticularexpectationwasmet.
Namespace:Moq.LanguageAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
voidVerifiable()
Copy
ExamplesThefollowingexamplemarkstheexpectationasverifiable:
C#
mock.Expect(x=>x.Execute("ping"))
.Returns(true)
.Verifiable();
SeeAlsoIVerifiesInterfaceMoq.LanguageNamespace
C#
MoqMoq.ProtectedNamespaceSendFeedbackImportingthisnamespacewillenabletheProtected()methodonmocks,enablingexpectationsonprotectedmembersbyspecifyingthemembernameasastring.
Classes Class Description
ItExpr
Allowsthespecificationofamatchingconditionforanargumentinaprotectedmemberexpectation,ratherthanaspecificargumentvalue."ItExpr"referstotheargumentbeingmatched.
ProtectedExtension
EnablestheProtected()methodonMock<(Of<(T>)>),allowingexpectationstobesetforprotectedmembersbyusingtheirnameasastring,ratherthanstrong-typingthemwhichisnotpossibleduetotheirvisibility.
Interfaces Interface Description
IProtectedMock
Allowsexpectationstobesetforprotectedmembersbyusingtheirnameasastring,ratherthanstrong-typingthemwhichisnotpossibleduetotheirvisibility.
C#
MoqIProtectedMockInterfaceMembersSeeAlsoSendFeedbackAllowsexpectationstobesetforprotectedmembersbyusingtheirnameasastring,ratherthanstrong-typingthemwhichisnotpossibleduetotheirvisibility.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicinterfaceIProtectedMock
SeeAlsoIProtectedMockMembersMoq.ProtectedNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIProtectedMockMembersIProtectedMockInterfaceMethodsSeeAlsoSendFeedback
TheIProtectedMocktypeexposesthefollowingmembers.
Methods Name Description
Expect Overloaded.ExpectGet<(Of<(TProperty>)>)
SetsanexpectationonapropertygetterwiththegivenpropertyName.
ExpectSet<(Of<(TProperty>)>)
SetsanexpectationonapropertysetterwiththegivenpropertyName.
SeeAlsoIProtectedMockInterfaceMoq.ProtectedNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIProtectedMockMethodsIProtectedMockInterfaceSeeAlsoSendFeedback
TheIProtectedMocktypeexposesthefollowingmembers.
Methods Name Description
Expect Overloaded.ExpectGet<(Of<(TProperty>)>)
SetsanexpectationonapropertygetterwiththegivenpropertyName.
ExpectSet<(Of<(TProperty>)>)
SetsanexpectationonapropertysetterwiththegivenpropertyName.
SeeAlsoIProtectedMockInterfaceMoq.ProtectedNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqIProtectedMock..::.ExpectMethodIProtectedMockInterfaceSeeAlsoSendFeedback
OverloadList Name Description
Expect<(Of<(TResult>)>)(String,array<Object>[]()[])
SetsanexpectationonapropertyoranonvoidmethodwiththegivenmethodOrPropertyName,optionallyspecifyingargumentsforthemethodcall.
Expect(String,array<Object>[]()[])
SetsanexpectationonthevoidmethodwiththegivenvoidMethodName,optionallyspecifyingargumentsforthemethodcall.
SeeAlsoIProtectedMockInterfaceIProtectedMockMembersMoq.ProtectedNamespace
C#
MoqIProtectedMock..::.Expect<(Of<(TResult>)>)Method(,array<>[]()[])IProtectedMockInterfaceSeeAlsoSendFeedbackSetsanexpectationonapropertyoranonvoidmethodwiththegivenmethodOrPropertyName,optionallyspecifyingargumentsforthemethodcall.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IExpect<TResult>Expect<TResult>(
stringmethodOrPropertyName,
paramsObject[]args
)
Parameters
methodOrPropertyNameType:StringNameofthemethodorpropertytobeinvoke.
argsType:array<Object>[]()[]Optionalargumentsfortheinvocation.
TypeParametersTResult
Returntypeofthemethodorproperty.
SeeAlsoIProtectedMockInterfaceExpectOverloadMoq.ProtectedNamespace
C#
MoqIProtectedMock..::.ExpectMethod(,array<>[]()[])IProtectedMockInterfaceSeeAlsoSendFeedbackSetsanexpectationonthevoidmethodwiththegivenvoidMethodName,optionallyspecifyingargumentsforthemethodcall.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IExpectExpect(
stringvoidMethodName,
paramsObject[]args
)
Parameters
voidMethodNameType:StringNameofthevoidmethodtobeinvoke.
argsType:array<Object>[]()[]Optionalargumentsfortheinvocation.
SeeAlsoIProtectedMockInterfaceExpectOverloadMoq.ProtectedNamespace
C#
MoqIProtectedMock..::.ExpectGet<(Of<(TProperty>)>)MethodIProtectedMockInterfaceSeeAlsoSendFeedbackSetsanexpectationonapropertygetterwiththegivenpropertyName.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IExpectGetter<TProperty>ExpectGet<TProperty>(
stringpropertyName
)
Parameters
propertyNameType:StringNameoftheproperty.
TypeParametersTProperty
Typeoftheproperty.
SeeAlsoIProtectedMockInterfaceMoq.ProtectedNamespace
C#
MoqIProtectedMock..::.ExpectSet<(Of<(TProperty>)>)MethodIProtectedMockInterfaceSeeAlsoSendFeedbackSetsanexpectationonapropertysetterwiththegivenpropertyName.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
IExpectSetter<TProperty>ExpectSet<TProperty>(
stringpropertyName
)
Parameters
propertyNameType:StringNameoftheproperty.
TypeParametersTProperty
Typeoftheproperty.
SeeAlsoIProtectedMockInterfaceMoq.ProtectedNamespace
C#
MoqItExprClassMembersSeeAlsoSendFeedbackAllowsthespecificationofamatchingconditionforanargumentinaprotectedmemberexpectation,ratherthanaspecificargumentvalue."ItExpr"referstotheargumentbeingmatched.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticclassItExpr
RemarksUsethisvariantofargumentmatchinginsteadofItforprotectedexpectations.
Thisclassallowstheexpectationtomatchamethodinvocationwithanarbitraryvalue,withavalueinaspecifiedrange,orevenonethatmatchesagivenpredicate.
InheritanceHierarchyObjectMoq.Protected..::.ItExpr
SeeAlsoItExprMembersMoq.ProtectedNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqItExprMembersItExprClassMethodsSeeAlsoSendFeedback
TheItExprtypeexposesthefollowingmembers.
Methods Name Description
Is<(Of<(TValue>)>) Matchesanyvaluethatsatisfiesthegivenpredicate.
IsAny<(Of<(TValue>)>) MatchesanyvalueofthegivenTValuetype.IsInRange<(Of<(TValue>)>)
Matchesanyvaluethatisintherangespecified.
IsRegex Overloaded.
SeeAlsoItExprClassMoq.ProtectedNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqItExprMethodsItExprClassSeeAlsoSendFeedback
TheItExprtypeexposesthefollowingmembers.
Methods Name Description
Is<(Of<(TValue>)>) Matchesanyvaluethatsatisfiesthegivenpredicate.
IsAny<(Of<(TValue>)>) MatchesanyvalueofthegivenTValuetype.IsInRange<(Of<(TValue>)>)
Matchesanyvaluethatisintherangespecified.
IsRegex Overloaded.
SeeAlsoItExprClassMoq.ProtectedNamespace
C#
MoqItExpr..::.Is<(Of<(TValue>)>)MethodItExprClassExampleSeeAlsoSendFeedbackMatchesanyvaluethatsatisfiesthegivenpredicate.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticExpressionIs<TValue>(
Expression<Predicate<TValue>>match
)
Parameters
matchType:Expression<(Of<(Predicate<(Of<(TValue>)>)>)>)Thepredicateusedtomatchthemethodargument.
TypeParametersTValue
Typeoftheargumenttocheck.
RemarksAllowsthespecificationofapredicatetoperformmatchingofmethodcallarguments.
Copy
Copy
ExamplesThisexampleshowshowtoreturnthevalue1whenevertheargumenttotheDomethodisanevennumber.
C#
mock.Protected()
.Expect("Do",ItExpr.Is<int>(i=>i%2==0))
.Returns(1);
Thisexampleshowshowtothrowanexceptioniftheargumenttothemethodisanegativenumber:
C#
mock.Protected()
.Expect("GetUser",ItExpr.Is<int>(i=>i<0))
.Throws(newArgumentException());
SeeAlsoItExprClassMoq.ProtectedNamespace
C#
MoqItExpr..::.IsAny<(Of<(TValue>)>)MethodItExprClassExampleSeeAlsoSendFeedbackMatchesanyvalueofthegivenTValuetype.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticExpressionIsAny<TValue>()
TypeParametersTValue
Typeofthevalue.
RemarksTypicallyusedwhentheactualargumentvalueforamethodcallisnotrelevant.
Copy
ExamplesC#
//ThrowsanexceptionforacalltoRemovewithanystringvalue.
mock.Protected()
.Expect("Remove",ItExpr.IsAny<string>())
.Throws(newInvalidOperationException());
SeeAlsoItExprClassMoq.ProtectedNamespace
C#
MoqItExpr..::.IsInRange<(Of<(TValue>)>)MethodItExprClassExampleSeeAlsoSendFeedbackMatchesanyvaluethatisintherangespecified.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticExpressionIsInRange<TValue>(
TValuefrom,
TValueto,
RangerangeKind
)
whereTValue:IComparable
Parameters
fromType:TValueThelowerboundoftherange.
toType:TValueTheupperboundoftherange.
rangeKindType:Moq..::.RangeThekindofrange.SeeRange.
TypeParametersTValue
Typeoftheargumenttocheck.
Copy
ExamplesThefollowingexampleshowshowtoexpectamethodcallwithanintegerargumentwithinthe0..100range.
C#
mock.Protected()
.Expect("HasInventory",
ItExpr.IsAny<string>(),
ItExpr.IsInRange(0,100,Range.Inclusive))
.Returns(false);
SeeAlsoItExprClassMoq.ProtectedNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqItExpr..::.IsRegexMethodItExprClassSeeAlsoSendFeedback
OverloadList Name Description
IsRegex(String) Matchesastringargumentifitmatchesthegivenregularexpressionpattern.
IsRegex(String,RegexOptions)
Matchesastringargumentifitmatchesthegivenregularexpressionpattern.
SeeAlsoItExprClassItExprMembersMoq.ProtectedNamespace
C#
MoqItExpr..::.IsRegexMethod()ItExprClassExampleSeeAlsoSendFeedbackMatchesastringargumentifitmatchesthegivenregularexpressionpattern.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticExpressionIsRegex(
stringregex
)
Parameters
regexType:StringThepatterntousetomatchthestringargumentvalue.
Copy
ExamplesThefollowingexampleshowshowtoexpectacalltoamethodwherethestringargumentmatchesthegivenregularexpression:
C#
mock.Protected()
.Expect("Check",ItExpr.IsRegex("[a-z]+"))
.Returns(1);
SeeAlsoItExprClassIsRegexOverloadMoq.ProtectedNamespace
C#
MoqItExpr..::.IsRegexMethod(,)ItExprClassExampleSeeAlsoSendFeedbackMatchesastringargumentifitmatchesthegivenregularexpressionpattern.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticExpressionIsRegex(
stringregex,
RegexOptionsoptions
)
Parameters
regexType:StringThepatterntousetomatchthestringargumentvalue.
optionsType:RegexOptionsTheoptionsusedtointerpretthepattern.
Copy
ExamplesThefollowingexampleshowshowtoexpectacalltoamethodwherethestringargumentmatchesthegivenregularexpression,inacaseinsensitiveway:
C#
mock.Protected()
.Expect("Check",ItExpr.IsRegex("[a-z]+",RegexOptions.IgnoreCase))
.Returns(1);
SeeAlsoItExprClassIsRegexOverloadMoq.ProtectedNamespace
C#
MoqProtectedExtensionClassMembersSeeAlsoSendFeedbackEnablestheProtected()methodonMock<(Of<(T>)>),allowingexpectationstobesetforprotectedmembersbyusingtheirnameasastring,ratherthanstrong-typingthemwhichisnotpossibleduetotheirvisibility.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticclassProtectedExtension
InheritanceHierarchyObjectMoq.Protected..::.ProtectedExtension
SeeAlsoProtectedExtensionMembersMoq.ProtectedNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqProtectedExtensionMembersProtectedExtensionClassMethodsSeeAlsoSendFeedback
TheProtectedExtensiontypeexposesthefollowingmembers.
Methods Name Description
Protected<(Of<(T>)>) Enableprotectedexpectationsforthemock.
SeeAlsoProtectedExtensionClassMoq.ProtectedNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqProtectedExtensionMethodsProtectedExtensionClassSeeAlsoSendFeedback
TheProtectedExtensiontypeexposesthefollowingmembers.
Methods Name Description
Protected<(Of<(T>)>) Enableprotectedexpectationsforthemock.
SeeAlsoProtectedExtensionClassMoq.ProtectedNamespace
C#
MoqProtectedExtension..::.Protected<(Of<(T>)>)MethodProtectedExtensionClassSeeAlsoSendFeedbackEnableprotectedexpectationsforthemock.
Namespace:Moq.ProtectedAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticIProtectedMockProtected<T>(
Mock<T>mock
)
whereT:class
Parameters
mockType:Moq..::.Mock<(Of<(T>)>)Themocktosettheprotectedexpectationson.
TypeParametersT
Mockedobjecttype.Typicallyomittedasitcanbeinferredfromthemockinstance.
SeeAlsoProtectedExtensionClassMoq.ProtectedNamespace
C#
MoqMoq.StubNamespaceSendFeedback
[Missing<summary>documentationforN:Moq.Stub]
Classes Class Description
StubExtensions AddsStubextensionmethodtoamocksothatyoucanstubproperties.
C#
MoqStubExtensionsClassMembersSeeAlsoSendFeedbackAddsStubextensionmethodtoamocksothatyoucanstubproperties.
Namespace:Moq.StubAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticclassStubExtensions
InheritanceHierarchyObjectMoq.Stub..::.StubExtensions
SeeAlsoStubExtensionsMembersMoq.StubNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqStubExtensionsMembersStubExtensionsClassMethodsSeeAlsoSendFeedback
TheStubExtensionstypeexposesthefollowingmembers.
Methods Name Description
Stub Overloaded.
StubAll<(Of<(T>)>)
Stubsallpropertiesonthemock,settingthedefaultvaluetotheonegeneratedasspecifiedbytheDefaultValueproperty.
SeeAlsoStubExtensionsClassMoq.StubNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqStubExtensionsMethodsStubExtensionsClassSeeAlsoSendFeedback
TheStubExtensionstypeexposesthefollowingmembers.
Methods Name Description
Stub Overloaded.
StubAll<(Of<(T>)>)
Stubsallpropertiesonthemock,settingthedefaultvaluetotheonegeneratedasspecifiedbytheDefaultValueproperty.
SeeAlsoStubExtensionsClassMoq.StubNamespace
C#IncludeProtectedMembersIncludeInheritedMembers
MoqStubExtensions..::.StubMethodStubExtensionsClassSeeAlsoSendFeedback
OverloadList Name Description
Stub<(Of<(T,TProperty>)>)(Mock<(Of<(T>)>),Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>))
Specifiesthatthegivenpropertyshouldhavestubbehavior,meaningthatsettingitsvaluewillcauseittobesavedandlaterreturnedwhenthepropertyisrequested.
Stub<(Of<(T,TProperty>)>)(Mock<(Of<(T>)>),Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>),TProperty)
Specifiesthatthegivenpropertyshouldhavestubbehavior,meaningthatsettingitsvaluewillcauseittobesavedandlaterreturnedwhenthepropertyisrequested.Thisoverloadallowssettingtheinitialvaluefortheproperty.
SeeAlsoStubExtensionsClassStubExtensionsMembersMoq.StubNamespace
C#
MoqStubExtensions..::.Stub<(Of<(T,TProperty>)>)Method(Mock<(Of<(T>)>),<(Of<(<(Of<(T,TProperty>)>)>)>))StubExtensionsClassExampleSeeAlsoSendFeedbackSpecifiesthatthegivenpropertyshouldhavestubbehavior,meaningthatsettingitsvaluewillcauseittobesavedandlaterreturnedwhenthepropertyisrequested.
Namespace:Moq.StubAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticvoidStub<T,TProperty>(
Mock<T>mock,
Expression<Func<T,TProperty>>property
)
whereT:class
Parameters
mockType:Moq..::.Mock<(Of<(T>)>)Theinstancetostub.
propertyType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Propertyexpressiontostub.
TypeParametersT
Mockedtype,inferredfromtheobjectwherethismethodisbeingapplied(doesnotneedtobespecified).
TPropertyTypeoftheproperty,inferredfromthepropertyexpression(doesnotneedtobespecified).
Copy
Copy
ExamplesIfyouhaveaninterfacewithanintpropertyValue,youmightstubitusingthefollowingstraightforwardcall:
C#
varmock=newMock<IHaveValue>();
mock.Stub(v=>v.Value);
AftertheStubcallhasbeenissued,settingandretrievingtheobjectvaluewillbehaveasexpected:
C#
IHaveValuev=mock.Object;
v.Value=5;
Assert.Equal(5,v.Value);
SeeAlsoStubExtensionsClassStubOverloadMoq.StubNamespace
C#
MoqStubExtensions..::.Stub<(Of<(T,TProperty>)>)Method(Mock<(Of<(T>)>),<(Of<(<(Of<(T,TProperty>)>)>)>),TProperty)StubExtensionsClassExampleSeeAlsoSendFeedbackSpecifiesthatthegivenpropertyshouldhavestubbehavior,meaningthatsettingitsvaluewillcauseittobesavedandlaterreturnedwhenthepropertyisrequested.Thisoverloadallowssettingtheinitialvaluefortheproperty.
Namespace:Moq.StubAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticvoidStub<T,TProperty>(
Mock<T>mock,
Expression<Func<T,TProperty>>property,
TPropertyinitialValue
)
whereT:class
Parameters
mockType:Moq..::.Mock<(Of<(T>)>)Theinstancetostub.
propertyType:Expression<(Of<(Func<(Of<(T,TProperty>)>)>)>)Propertyexpressiontostub.
initialValueType:TPropertyInitialvaluefortheproperty.
TypeParametersT
Mockedtype,inferredfromtheobjectwherethismethodisbeingapplied(doesnotneedtobespecified).
TPropertyTypeoftheproperty,inferredfromthepropertyexpression(doesnotneedtobespecified).
Copy
Copy
ExamplesIfyouhaveaninterfacewithanintpropertyValue,youmightstubitusingthefollowingstraightforwardcall:
C#
varmock=newMock<IHaveValue>();
mock.Stub(v=>v.Value,5);
AftertheStubcallhasbeenissued,settingandretrievingtheobjectvaluewillbehaveasexpected:
C#
IHaveValuev=mock.Object;
//Initialvaluewasstored
Assert.Equal(5,v.Value);
//Newvaluesetwhichchangestheinitialvalue
v.Value=6;
Assert.Equal(6,v.Value);
SeeAlsoStubExtensionsClassStubOverloadMoq.StubNamespace
C#
MoqStubExtensions..::.StubAll<(Of<(T>)>)MethodStubExtensionsClassSeeAlsoSendFeedbackStubsallpropertiesonthemock,settingthedefaultvaluetotheonegeneratedasspecifiedbytheDefaultValueproperty.
Namespace:Moq.StubAssembly:Moq(inMoq.dll)Version:2.6.1014.1(2.6.0.0)
SyntaxC#
publicstaticvoidStubAll<T>(
Mock<T>mock
)
whereT:class
Parameters
mockType:Moq..::.Mock<(Of<(T>)>)Themocktostub.
TypeParametersT
Mockedtype,typicallyomittedasitcanbeinferredfromthemockargument.
RemarksIfthemockDefaultValueissettoMock,themockeddefaultvalueswillalsobestubbedrecursively.
SeeAlsoStubExtensionsClassMoq.StubNamespace