public RemotingProxy(Type type, IInterceptor interceptor, IMockedObject mockedObject) : base(type) { _interceptor = interceptor; _mockedObject = mockedObject; }
/// <summary> /// Initializes a new instance of the <see cref="RhinoInterceptor"/> class. /// Creates a new <see cref="RhinoInterceptor"/> instance. /// </summary> /// <param name="repository"> /// The repository. /// </param> /// <param name="proxyInstance"> /// The proxy Instance. /// </param> /// <param name="invocation_visitors"> /// The invocation_visitors. /// </param> public RhinoInterceptor(MockRepository repository, IMockedObject proxyInstance, IEnumerable<InvocationVisitor> invocation_visitors) { this.repository = repository; this.proxyInstance = proxyInstance; this.invocation_visitors = invocation_visitors; }
/// <summary> /// Creates a new <see cref="RecordMockState"/> instance. /// </summary> /// <param name="repository">Repository.</param> /// <param name="mockedObject">The proxy that generates the method calls</param> public RecordMockState(IMockedObject mockedObject, MockRepository repository) { Validate.IsNotNull(mockedObject, "proxy"); Validate.IsNotNull(repository, "repository"); this.repository = repository; this.mockedObject = mockedObject; }
/// <summary> /// Compares two instances of mocked objects /// </summary> public int Compare(object x, object y) { if (x == null && y == null) { return(0); } if (x == null) { return(1); } if (y == null) { return(-1); } IMockedObject one = MockRepository.GetMockedObjectOrNull(x); IMockedObject two = MockRepository.GetMockedObjectOrNull(y); if (one == null && two == null) { return(-2);//both of them are probably transperant proxies } if (one == null) { return(1); } if (two == null) { return(-1); } return(one.ProxyHash - two.ProxyHash); }
/// <summary> /// Reset the selected expectation on this mock object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mock">The mock.</param> /// <param name="options">The options to reset the expectations on this mock.</param> public static void BackToRecord <T>(this T mock, BackToRecordOptions options) { IMockedObject mockedObject = MockRepository.GetMockedObject(mock); var mocks = mockedObject.Repository; mocks.BackToRecord(mock, options); }
/// <summary> /// Initializes a new instance of the <see cref="StubRecordMockState"/> class. /// </summary> /// <param name="mockedObject">The proxy that generates the method calls</param> /// <param name="repository">Repository.</param> /// <param name="isPartial">A flag indicating whether we should behave like a partial stub.</param> public StubRecordMockState(IMockedObject mockedObject, MockRepository repository, bool isPartial) : base(mockedObject, repository) { this.isPartial = isPartial; Type[] types = mockedObject.ImplementedTypes; SetPropertyBehavior(mockedObject, types); }
/// <summary> /// Creates a new <see cref="RecordMockState"/> instance. /// </summary> /// <param name="repository">Repository.</param> /// <param name="mockedObject">The proxy that generates the method calls</param> public RecordMockState(IMockedObject mockedObject, MockRepository repository) { Validate.IsNotNull(mockedObject, "proxy"); Validate.IsNotNull(repository, "repository"); this.repository = repository; this.mockedObject = mockedObject; expectationBuilder = new ExpectationBuilder(); }
public void MockObjCanHandleGetEventSubscribersCallsWithoutEventsRegistered() { WithParameters withParameters = MockRepository.GenerateStrictMock <WithParameters>(1); IMockedObject mocked = (IMockedObject)withParameters; Delegate eventSubscribers = mocked.GetEventSubscribers("ff"); Assert.Null(eventSubscribers); }
/// <summary> /// Get the hash code for a proxy object without calling GetHashCode() /// on the object. /// </summary> public int GetHashCode(object obj) { IMockedObject mockedObject = MockRepository.GetMockedObjectOrNull(obj); if (mockedObject == null) { return(obj.GetHashCode()); } return(mockedObject.ProxyHash); }
///// <summary> ///// Create an expectation on this mock for this action to occur ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="mock">The mock.</param> ///// <param name="action">The action.</param> ///// <returns></returns> //public static IMethodOptions<VoidType> Expect<T>(this T mock, Action<T> action) // where T : class //{ // return Expect<T, VoidType>(mock, t => // { // action(t); // return null; // }); //} ///// <summary> ///// Reset all expectations on this mock object ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="mock">The mock.</param> //public static void BackToRecord<T>(this T mock) //{ // BackToRecord(mock, BackToRecordOptions.All); //} /// <summary> /// Reset the selected expectation on this mock object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mock">The mock.</param> ///// <param name="options">The options to reset the expectations on this mock.</param> //public static void BackToRecord<T>(this T mock, BackToRecordOptions options) //{ // IMockedObject mockedObject = MockRepository.GetMockedObject(mock); // var mocks = mockedObject.Repository; // mocks.BackToRecord(mock, options); //} /// <summary> /// Cause the mock state to change to replay, any further call is compared to the /// ones that were called in the record state. /// </summary> /// <param name="mock">the mocked object to move to replay state</param> public static void Replay <T>(this T mock) { IMockedObject mockedObject = MockRepository.GetMockedObject(mock); var mocks = mockedObject.Repository; if (mocks.IsInReplayMode(mock) != true) { mocks.Replay(mockedObject); } }
///<summary> /// Create the proxy using remoting ///</summary> public object CreateRemotingMock(Type type, IInterceptor interceptor, IMockedObject mockedObject) { if (type.IsInterface == false && !typeof(MarshalByRefObject).IsAssignableFrom(type)) { throw new InvalidCastException( String.Format("Cannot create remoting proxy. '{0}' is not derived from MarshalByRefObject", type.Name)); } return new RemotingProxy(type, interceptor, mockedObject).GetTransparentProxy(); }
///<summary> /// Create an event raiser for the specified event on this instance. ///</summary> public static IEventRaiser Create(object instance, string eventName) { IMockedObject proxy = instance as IMockedObject; if (proxy == null) { throw new ArgumentException("Parameter must be a mocked object", "instance"); } return(new EventRaiser(proxy, eventName)); }
public void GetmocksFromProxy() { IMockedObject mockedObj = demo as IMockedObject; Assert.NotNull(mockedObj); MockRepository MockRepository = mockedObj.Repository; Assert.NotNull(MockRepository); Assert.Same(mocks, MockRepository); }
///<summary> /// Create the proxy using remoting ///</summary> public object CreateRemotingMock(Type type, IInterceptor interceptor, IMockedObject mockedObject) { if (type.IsInterface == false && !typeof(MarshalByRefObject).IsAssignableFrom(type)) { throw new InvalidCastException( String.Format("Cannot create remoting proxy. '{0}' is not derived from MarshalByRefObject", type.Name)); } return(new RemotingProxy(type, interceptor, mockedObject).GetTransparentProxy()); }
// we need to ensure that we won't re-eenterant into the repository // if the parameter is a mock object private static string MockingSafeToString(object arg) { IMockedObject mock = arg as IMockedObject; if (mock == null) { return(arg.ToString()); } return(mock.GetType().BaseType.FullName); }
/// <summary> /// Creates a new <see cref="IMethodOptions{T}"/> instance. /// </summary> /// <param name="repository">the repository for this expectation</param> /// <param name="record">the recorder for this proxy</param> /// <param name="proxy">the proxy for this expectation</param> /// <param name="expectation">Expectation.</param> public MethodOptions( MockRepository repository, RecordMockState record, IMockedObject proxy, IExpectation expectation) { this.expectation = expectation; this.proxy = proxy; this.repository = repository; this.record = record; }
internal static IMockedObject AsMockObject(this object mockedInstance) { IMockedObject mockedObj = mockedInstance.AsMockObjectOrNull(); if (mockedObj == null) { throw new InvalidOperationException("The object '" + mockedInstance + "' is not a mocked object."); } return(mockedObj); }
///<summary> ///</summary> public IEnumerable<InvocationVisitor> CreateStandardInvocationVisitors(IMockedObject proxy_instance, MockRepository mockRepository) { List<InvocationVisitor> invocation_visitors = new List<InvocationVisitor>(); invocation_visitors.Add(new InvocationVisitor(new IsAnInvocationOfAMethodBelongingToObject(), new Proceed())); invocation_visitors.Add(new InvocationVisitor(new IsAnInvocationOnAMockedObject(), new InvokeMethodAgainstMockedObject(proxy_instance))); invocation_visitors.Add(new InvocationVisitor(new IsInvocationThatShouldTargetOriginal(proxy_instance), new Proceed())); invocation_visitors.Add(new InvocationVisitor(new IsAPropertyInvocation(proxy_instance), new InvokeProperty(proxy_instance, mockRepository))); return invocation_visitors; }
/// <summary> /// This method is safe for use even if any of the objects is a mocked object /// that override equals. /// </summary> private static bool SafeEquals(object expected, object actual) { IMockedObject expectedMock = expected as IMockedObject; IMockedObject actualMock = actual as IMockedObject; //none are mocked object if (expectedMock == null && actualMock == null) { return(expected.Equals(actual)); } //if any of them is a mocked object, use mocks equality //this may not be what the user is expecting, but it is needed, because //otherwise we get into endless loop. return(MockedObjectsEquality.Instance.Equals(expected, actual)); }
///<summary> ///</summary> public IEnumerable <InvocationVisitor> CreateStandardInvocationVisitors(IMockedObject proxy_instance, MockRepository mockRepository) { List <InvocationVisitor> invocation_visitors = new List <InvocationVisitor>(); invocation_visitors.Add(new InvocationVisitor(new IsAnInvocationOfAMethodBelongingToObject(), new Proceed())); invocation_visitors.Add(new InvocationVisitor(new IsAnInvocationOnAMockedObject(), new InvokeMethodAgainstMockedObject(proxy_instance))); invocation_visitors.Add(new InvocationVisitor(new IsInvocationThatShouldTargetOriginal(proxy_instance), new Proceed())); invocation_visitors.Add(new InvocationVisitor(new IsAPropertyInvocation(proxy_instance), new InvokeProperty(proxy_instance, mockRepository))); return(invocation_visitors); }
/// <summary> /// Finds the approprite implementation type of this item. /// This is the class or an interface outside of the rhino mocks. /// </summary> /// <param name="mockedObj">The mocked obj.</param> /// <returns></returns> private static Type FindAppropriteType <T>(IMockedObject mockedObj) { foreach (var type in mockedObj.ImplementedTypes) { if (type.IsClass && typeof(T).IsAssignableFrom(type)) { return(type); } } foreach (var type in mockedObj.ImplementedTypes) { if (type.Assembly == typeof(IMockedObject).Assembly || !typeof(T).IsAssignableFrom(type)) { continue; } return(type); } return(mockedObj.ImplementedTypes[0]); }
private static ExpectationVerificationInformation GetExpectationsToVerify <T>(T mock, Action <T> action, Action <IMethodOptions <object> > setupConstraints) { IMockedObject mockedObject = MockRepository.GetMockedObject(mock); MockRepository mocks = mockedObject.Repository; if (mocks.IsInReplayMode(mockedObject) == false) { throw new InvalidOperationException( "Cannot assert on an object that is not in replay mode. Did you forget to call ReplayAll() ?"); } var mockToRecordExpectation = (T)mocks.DynamicMock(FindAppropriteType <T>(mockedObject), mockedObject.ConstructorArguments); action(mockToRecordExpectation); AssertExactlySingleExpectaton(mocks, mockToRecordExpectation); IMethodOptions <object> lastMethodCall = mocks.LastMethodCall <object>(mockToRecordExpectation); lastMethodCall.TentativeReturn(); if (setupConstraints != null) { setupConstraints(lastMethodCall); } ExpectationsList expectationsToVerify = mocks.Replayer.GetAllExpectationsForProxy(mockToRecordExpectation); if (expectationsToVerify.Count == 0) { throw new InvalidOperationException( "The expectation was removed from the waiting expectations list, did you call Repeat.Any() ? This is not supported in AssertWasCalled()"); } IExpectation expected = expectationsToVerify[0]; ICollection <object[]> argumentsForAllCalls = mockedObject.GetCallArgumentsFor(expected.Method); return(new ExpectationVerificationInformation { ArgumentsForAllCalls = new List <object[]>(argumentsForAllCalls), Expected = expected }); }
static void SetPropertyBehavior(IMockedObject mockedObject, IEnumerable<Type> implementedTypes) { foreach (Type type in implementedTypes) { if (type.BaseType != null && type.BaseType != typeof(object)) SetPropertyBehavior(mockedObject, new[] { type.BaseType }); SetPropertyBehavior(mockedObject, type.GetInterfaces()); foreach (PropertyInfo propertyInfo in type.GetProperties()) { if (propertyInfo.CanRead && CanWriteToPropertyThroughPublicSignature(propertyInfo) && !mockedObject.RegisterPropertyBehaviorFor(propertyInfo) && propertyInfo.PropertyType.IsValueType) { CreateDefaultValueForValueTypeProperty(mockedObject, propertyInfo); } } } }
static void SetPropertyBehavior(IMockedObject mockedObject, IEnumerable <Type> implementedTypes) { foreach (Type type in implementedTypes) { if (type.BaseType != null && type.BaseType != typeof(object)) { SetPropertyBehavior(mockedObject, new[] { type.BaseType }); } SetPropertyBehavior(mockedObject, type.GetInterfaces()); foreach (PropertyInfo propertyInfo in type.GetProperties()) { if (propertyInfo.CanRead && CanWriteToPropertyThroughPublicSignature(propertyInfo) && !mockedObject.RegisterPropertyBehaviorFor(propertyInfo) && propertyInfo.PropertyType.IsValueType) { CreateDefaultValueForValueTypeProperty(mockedObject, propertyInfo); } } } }
/// <summary> /// Create an expectation on this mock for this action to occur /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="R"></typeparam> /// <param name="mock">The mock.</param> /// <param name="action">The action.</param> /// <returns></returns> public static IMethodOptions <R> Expect <T, R>(this T mock, Function <T, R> action) where T : class { if (mock == null) { throw new ArgumentNullException("mock", "You cannot mock a null instance"); } IMockedObject mockedObject = MockRepository.GetMockedObject(mock); MockRepository mocks = mockedObject.Repository; var isInReplayMode = mocks.IsInReplayMode(mock); mocks.BackToRecord(mock, BackToRecordOptions.None); action(mock); IMethodOptions <R> options = LastCall.GetOptions <R>(); options.TentativeReturn(); if (isInReplayMode) { mocks.ReplayCore(mock, false); } return(options); }
private void SetPropertyBehavior(IMockedObject mockedObject, params Type[] types) { foreach (Type implementedType in types) { if (implementedType.BaseType != null && implementedType.BaseType != typeof(object)) { SetPropertyBehavior(mockedObject, implementedType.BaseType); } SetPropertyBehavior(mockedObject, implementedType.GetInterfaces()); foreach (PropertyInfo property in implementedType.GetProperties()) { if (property.CanRead && CanWriteToPropertyThroughPublicSignature(property)) { bool alreadyHasValue = mockedObject.RegisterPropertyBehaviorFor(property); if (property.PropertyType.IsValueType && alreadyHasValue == false) { CreateDefaultValueForValueTypeProperty(mockedObject, property); } } } } }
public void Process(RemotingProxy proxy) { _mockedObject = proxy.MockedObject; }
/// <summary> /// Creates a new <see cref="RhinoInterceptor"/> instance. /// </summary> public RhinoInterceptor(MockRepository repository, IMockedObject proxyInstance) { this.repository = repository; this.proxyInstance = proxyInstance; }
///<summary> ///</summary> public InvokeProperty(IMockedObject proxy_instance, MockRepository mockRepository) { proxyInstance = proxy_instance; this.mockRepository = mockRepository; }
public VerifyExpectationAndCallOriginalRecordState(IMockedObject mockedObject, MockRepository repository) : base(mockedObject, repository) { }
/// <summary> /// Creates a new <see cref="ReplayMockState"/> instance. /// </summary> /// <param name="previousState">The previous state for this method</param> public ReplayMockState(RecordMockState previousState) { this.repository = previousState.Repository; this.proxy = previousState.Proxy; }
/// <summary> /// Creates a new <see cref="RecordDynamicMockState"/> instance. /// </summary> /// <param name="repository">Repository.</param> /// <param name="mockedObject">The proxy that generates the method calls</param> public RecordPartialMockState(IMockedObject mockedObject, MockRepository repository) : base(mockedObject, repository) { }
/// <summary> /// Gets the mock repository for this specificied mock object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mock">The mock.</param> /// <returns></returns> public static MockRepository GetMockRepository <T>(this T mock) { IMockedObject mockedObject = MockRepository.GetMockedObject(mock); return(mockedObject.Repository); }
/* * Method: On * Gets the method options for the last call for mockedInstance. * This is the recommended approach for multi threaded scenarios. * * Expected usage: * (start code) * LastCall.On(mockObj).Return(4); * (end) * * Thread safety: * This method is safe to use in multi threading scenarios. */ /// <summary> /// Allows to get an interface to work on the last call. /// </summary> /// <param name="mockedInstance">The mocked object</param> /// <returns>Interface that allows to set options for the last method call on this object</returns> public static IMethodOptions <object> On(object mockedInstance) { IMockedObject mockedObj = MockRepository.GetMockedObject(mockedInstance); return(mockedObj.Repository.LastMethodCall <object>(mockedInstance)); }
public static void RegisterPropertyBehavior(IMockedObject mockedObject) { SetPropertyBehavior(mockedObject, mockedObject.ImplementedTypes); }
///<summary> ///</summary> public HandleEvent(IMockedObject proxy_instance) { proxyInstance = proxy_instance; }
///<summary> ///</summary> public InvokeMethodAgainstMockedObject(IMockedObject proxy_instance) { proxyInstance = proxy_instance; }
private static void CreateDefaultValueForValueTypeProperty(IMockedObject mockedObject, PropertyInfo property) { mockedObject.HandleProperty(property.GetSetMethod(true), new object[] { Activator.CreateInstance(property.PropertyType) }); }
private void SetPropertyBehavior(IMockedObject mockedObject, params Type[] types) { foreach (Type implementedType in types) { if (implementedType.BaseType != null && implementedType.BaseType != typeof(object)) { SetPropertyBehavior(mockedObject, implementedType.BaseType); } SetPropertyBehavior(mockedObject, implementedType.GetInterfaces()); foreach (PropertyInfo property in implementedType.GetProperties()) { if (property.CanRead && property.CanWrite) { bool alreadyHasValue = mockedObject.RegisterPropertyBehaviorFor(property); if (property.PropertyType.IsValueType && alreadyHasValue == false) { //make sure that it creates a default value for value types mockedObject.HandleProperty(property.GetSetMethod(true), new object[] { Activator.CreateInstance(property.PropertyType) }); } } } } }
/// <summary> /// Initializes a new instance of the <see cref="StubRecordMockState"/> class. /// </summary> /// <param name="mockedObject">The proxy that generates the method calls</param> /// <param name="repository">Repository.</param> public StubRecordMockState(IMockedObject mockedObject, MockRepository repository) : base(mockedObject, repository) { Type[] types = mockedObject.ImplementedTypes; SetPropertyBehavior(mockedObject, types); }
/// <summary> /// Creates a new <see cref="RecordDynamicMockState"/> instance. /// </summary> /// <param name="repository">Repository.</param> /// <param name="mockedObject">The proxy that generates the method calls</param> public RecordDynamicMockState(IMockedObject mockedObject, MockRepository repository) : base(mockedObject, repository) { }
private IMockState CreateVerifyAndCallOriginalMockState(IMockedObject mockedObject) { return(new VerifyExpectationAndCallOriginalRecordState(mockedObject, this)); }
/// <summary> /// Verifies all expectations on this mock object /// </summary> /// <param name="mockObject">The mock object.</param> public static void VerifyAllExpectations(this object mockObject) { IMockedObject mockedObject = MockRepository.GetMockedObject(mockObject); mockedObject.Repository.Verify(mockedObject); }
/// <summary> /// Creates a new instance of <c>EventRaiser</c> /// </summary> public EventRaiser(IMockedObject proxy, string eventName) { this.eventName = eventName; this.proxy = proxy; }
static void CreateDefaultValueForValueTypeProperty(IMockedObject mockedObject, PropertyInfo property) { mockedObject.HandleProperty( property.GetSetMethod(true), new[] { Activator.CreateInstance(property.PropertyType) }); }