Ejemplo n.º 1
0
        private static BuildableExpectation BuildExpectation(
            string expectationDescription,
            string matchRequiredCallCountDescription,
            string matchMatchingCallCountDescription,
            IMockObject receiver,
            string matchMethodDescription,
            string matchArgumentsDescription,
            params string[] extraMatcherDescriptions)
        {
            var extraMatchers = new Matcher[extraMatcherDescriptions.Length];

            for (int i = 0; i < extraMatchers.Length; i++)
            {
                extraMatchers[i] = new AlwaysMatcher(true, extraMatcherDescriptions[i]);
            }

            return(BuildExpectation(
                       expectationDescription,
                       new AlwaysMatcher(true, matchRequiredCallCountDescription),
                       new AlwaysMatcher(true, matchMatchingCallCountDescription),
                       receiver,
                       new AlwaysMatcher(true, matchMethodDescription),
                       new AlwaysMatcher(true, matchArgumentsDescription),
                       extraMatchers));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Adds itself to the <paramref name="result"/> if the <see cref="Receiver"/> matches
 /// the specified <paramref name="mock"/>.
 /// </summary>
 /// <param name="mock">The mock for which expectations are queried.</param>
 /// <param name="result">The result to add matching expectations to.</param>
 public void QueryExpectationsBelongingTo(IMockObject mock, IList <IExpectation> result)
 {
     if (Receiver == mock)
     {
         result.Add(this);
     }
 }
Ejemplo n.º 3
0
        private void AddExpectationsToMockFactory()
        {
            IMockObject mockObjectControl = (IMockObject)_mock.MockObject;

            mockObjectControl.AddExpectation(_expectation1);
            mockObjectControl.AddExpectation(_expectation2);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Defines the receiver.
        /// </summary>
        /// <param name="receiver">The dynamic mock on which the expectation or stub is applied.</param>
        /// <returns>Method syntax defining the method, property or event.</returns>
        private void On(IMockObject receiver)
        {
            MockObject = receiver;

            BuildableExpectation.Receiver = MockObject;
            MockObject.AddExpectation(BuildableExpectation);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Clears all expectation on the specified mock.
        /// </summary>
        /// <param name="mockObject">The mock for which all expectations are cleared.</param>
        internal void ClearExpectations(IMockObject mockObject)
        {
            var result = new List <IExpectation>();

            _expectations.QueryExpectationsBelongingTo(mockObject, result);

            result.ForEach(expectation => _expectations.RemoveExpectation(expectation));
        }
Ejemplo n.º 6
0
        internal void AssertAll(IMockObject mockObject)
        {
            var result = new List <IExpectation>();

            _expectations.QueryExpectationsBelongingTo(mockObject, result);

            result.ForEach(_ => _.Assert());
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets or sets the mock style for the specified mock.
        /// </summary>
        /// <param name="mock">the mock object</param>
        /// <value>mock style. null if no value defined.</value>
        public MockStyle?this[IMockObject mock]
        {
            get
            {
                return(mockStyleForStub.ContainsKey(mock) ? mockStyleForStub[mock] : null);
            }

            set
            {
                mockStyleForStub[mock] = value;
            }
        }
Ejemplo n.º 8
0
		/// <summary>
		/// Gets or sets the mock style for the specified mock.
		/// </summary>
		/// <param name="mock">the mock object</param>
		/// <value>mock style. null if no value defined.</value>
		public MockStyle? this[IMockObject mock]
		{
			get
			{
				return mockStyleForStub.ContainsKey(mock) ? mockStyleForStub[mock] : null;
			}

			set
			{
				mockStyleForStub[mock] = value;
			}
		}
Ejemplo n.º 9
0
        public void CanMapTypesWithDefaultObjectBuilder()
        {
            WCSFBuilder builder = new WCSFBuilder();
            Locator     locator = CreateLocator();

            TypeMappingPolicy policy = new TypeMappingPolicy(typeof(MockObject), null);

            builder.Policies.Set <ITypeMappingPolicy>(policy, typeof(IMockObject), null);

            IMockObject obj = builder.BuildUp <IMockObject>(locator, null, null);

            Assert.IsTrue(obj is MockObject);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Casts the argument to <see cref="IMockObject"/>.
        /// </summary>
        /// <param name="mock">The object to cast.</param>
        /// <returns>The argument casted to <see cref="IMockObject"/></returns>
        /// <throws cref="ArgumentNullException">Thrown if <paramref name="mock"/> is null</throws>
        /// <throws cref="ArgumentException">Thrown if <paramref name="mock"/> is not a <see cref="IMockObject"/></throws>
        private static IMockObject CastToMockObject(object mock)
        {
            if (mock == null)
            {
                throw new ArgumentNullException("mock", "mock must not be null");
            }

            IMockObject mockObject = mock as IMockObject;

            if (mockObject != null)
            {
                return(mockObject);
            }

            throw new ArgumentException("argument must be a mock", "mock");
        }
Ejemplo n.º 11
0
        private IVdsServiceLoader GetMockVdsServiceLoader()
        {
            IVdsServiceLoader mockObject = null;
            Assembly          assembly   = null;

            try
            {
                assembly = Assembly.Load("Microsoft.Storage.Vds.TestModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
            }
            catch (Exception exception)
            {
                if (VdsException.IsFatalException(exception))
                {
                    throw;
                }
            }
            Type type = null;

            if (assembly != null)
            {
                type = assembly.GetType("Microsoft.Storage.Vds.TestModel.TmVdsLoader", false);
            }
            if (type != null)
            {
                try
                {
                    IMockObject obj3 = type.InvokeMember("GetInstance", BindingFlags.InvokeMethod, null, null, new object[0], CultureInfo.InvariantCulture) as IMockObject;
                    if (obj3 != null)
                    {
                        mockObject = obj3.GetMockObject() as IVdsServiceLoader;
                    }
                }
                catch (Exception exception2)
                {
                    if (VdsException.IsFatalException(exception2))
                    {
                        throw;
                    }
                }
            }
            return(mockObject);
        }
Ejemplo n.º 12
0
        private static BuildableExpectation BuildExpectation(
            string description,
            Matcher requiredCallCountMatcher,
            Matcher matchingCallCountMatcher,
            IMockObject receiver,
            Matcher methodMatcher,
            Matcher argumentsMatcher,
            params Matcher[] extraMatchers)
        {
            var e = new BuildableExpectation(description, requiredCallCountMatcher, matchingCallCountMatcher);

            e.ArgumentsMatcher = argumentsMatcher;
            e.MethodMatcher    = methodMatcher;
            e.AddAction(Return.Value(new object()));
            e.Receiver = receiver;
            foreach (Matcher extraMatcher in extraMatchers)
            {
                e.AddInvocationMatcher(extraMatcher);
            }
            return(e);
        }
Ejemplo n.º 13
0
		/// <summary>
		/// Gets or sets the mock style for the specified mock and type.
		/// </summary>
		/// <param name="mock">the mock object</param>
		/// <param name="nestedMockType">the type of the nested mock.</param>
		/// <value>mock style. null if no value defined.</value>
		public MockStyle? this[IMockObject mock, Type nestedMockType]
		{
			get
			{
				Key key = new Key(mock, nestedMockType);

				if (mockStyleForType.ContainsKey(key))
				{
					return mockStyleForType[key] ?? mockStyleForStub[mock];
				}

				return this[mock];
			}

			set
			{
				Key key = new Key(mock, nestedMockType);

				mockStyleForType[key] = value;
			}
		}
Ejemplo n.º 14
0
        /// <summary>
        /// Gets or sets the mock style for the specified mock and type.
        /// </summary>
        /// <param name="mock">the mock object</param>
        /// <param name="nestedMockType">the type of the nested mock.</param>
        /// <value>mock style. null if no value defined.</value>
        public MockStyle?this[IMockObject mock, Type nestedMockType]
        {
            get
            {
                Key key = new Key(mock, nestedMockType);

                if (mockStyleForType.ContainsKey(key))
                {
                    return(mockStyleForType[key] ?? mockStyleForStub[mock]);
                }

                return(this[mock]);
            }

            set
            {
                Key key = new Key(mock, nestedMockType);

                mockStyleForType[key] = value;
            }
        }
Ejemplo n.º 15
0
        private static object BuildExpectation(
            bool matchRequiredCallCount,
            bool matchMatchingCallCount,
            IMockObject receiver,
            bool matchMethod,
            bool matchArguments,
            params bool[] matchExtraMatchers)
        {
            Matcher[] extraMatchers = new Matcher[matchExtraMatchers.Length];
            for (int i = 0; i < extraMatchers.Length; i++)
            {
                extraMatchers[i] = new AlwaysMatcher(matchExtraMatchers[i], "extra matcher " + (i + 1));
            }

            return(BuildExpectation(
                       "description",
                       new AlwaysMatcher(matchRequiredCallCount, "required call count"),
                       new AlwaysMatcher(matchMatchingCallCount, "matching call count"),
                       receiver,
                       new AlwaysMatcher(matchMethod, "method"),
                       new AlwaysMatcher(matchArguments, "argument"),
                       extraMatchers));
        }
Ejemplo n.º 16
0
        public void AssertionExceptionThrownWhenSomeExpectationsHaveNotBeenMetContainsDescriptionOfUnMetExpectations()
        {
            IMockObject mockObjectControl = (IMockObject)_mock.MockObject;

            MockExpectation expectation3 = new MockExpectation();

            _expectation1.Description = "expectation1";
            _expectation1.HasBeenMet  = false;
            _expectation1.IsActive    = true;
            _expectation2.Description = "expectation2";
            _expectation2.HasBeenMet  = true;
            _expectation2.IsActive    = true;
            expectation3.Description  = "expectation3";
            expectation3.HasBeenMet   = false;
            expectation3.IsActive     = true;

            mockObjectControl.AddExpectation(_expectation1);
            mockObjectControl.AddExpectation(_expectation2);
            mockObjectControl.AddExpectation(expectation3);

            try
            {
                _mockFactory.VerifyAllExpectationsHaveBeenMet();
            }
            catch (UnmetExpectationException e)
            {
                string newLine = Environment.NewLine;

                Assert.AreEqual(
                    "Not all expected invocations were performed." + newLine +
                    "MockFactory Expectations:" + newLine +
                    "  expectation1" + newLine +
                    "  expectation3" + newLine,
                    e.Message);
            }
        }
Ejemplo n.º 17
0
		public ReturnPropertyValueAction(IMockObject mockObject)
		{
			_mockObject = mockObject;
		}
Ejemplo n.º 18
0
			/// <summary>
			/// Initializes a new instance of the <see cref="Key"/> class.
			/// </summary>
			/// <param name="mock">The mock object.</param>
			/// <param name="nestedMockType">Type of the nested mock.</param>
			public Key(IMockObject mock, Type nestedMockType)
			{
				Mock = mock;
				NestedMockType = nestedMockType;
			}
Ejemplo n.º 19
0
 public void QueryExpectationsBelongingTo(IMockObject mock, IList <IExpectation> result)
 {
 }
Ejemplo n.º 20
0
        public void MockReturnsNameFromMockNameProperty()
        {
            IMockObject mock = (IMockObject)_mockFactory.CreateMock <IMockedType>("mock").MockObject;

            Assert.AreEqual("mock", mock.MockName);
        }
Ejemplo n.º 21
0
        public void ClassMockReturnsDefaultNameFromMockNameProperty()
        {
            IMockObject mock = (IMockObject)_mockFactory.CreateMock <SampleClass>().MockObject;

            Assert.AreEqual("sampleClass", mock.MockName);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Gets the mock style to be used for a mock created for a return value of a call to mock with stub behavior.
        /// </summary>
        /// <param name="mock">The mock that wants to create a mock.</param>
        /// <param name="requestedType">The type of the requested mock.</param>
        /// <returns>The mock style to use on the created mock. Null if <see cref="MockStyle.Default"/> has to be used.</returns>
        internal MockStyle?GetDependencyMockStyle(object mock, Type requestedType)
        {
            IMockObject mockObject = CastToMockObject(mock);

            return(_stubMockStyleDictionary[mockObject, requestedType]);
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Defines the receiver.
        /// </summary>
        /// <param name="receiver">The dynamic mock on which the expectation or stub is applied.</param>
        /// <returns>Method syntax defining the method, property or event.</returns>
        public IMethodSyntax On(object receiver) {
            if (receiver is IMockObject)
            {
                mockObject = (IMockObject) receiver;

                expectation.Receiver = mockObject;
                mockObject.AddExpectation(expectation);
            }
            else
            {
                throw new ArgumentException("not a mock object", "receiver");
            }

            return this;
        }
 public void SetUp()
 {
     var mockery = new Mockery();
     receiver = mockery.NewInstanceOfRole<IMockObject>(DefinedAs.Named("receiver"));
     invocation = new Invocation(receiver, new MethodInfoStub("method"), new object[] {"arg"});
 }
Ejemplo n.º 25
0
 public ReturnPropertyValueAction(IMockObject mockObject)
 {
     _mockObject = mockObject;
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Sets the mock style used for all properties and methods returning a value of type <paramref name="nestedMockType"/>
        /// of the <paramref name="mock"/>.
        /// </summary>
        /// <param name="mock">The mock (with mock style Stub).</param>
        /// <param name="nestedMockType">Type of the nested mock.</param>
        /// <param name="nestedMockStyle">The nested mock style.</param>
        public void SetStubMockStyle(object mock, Type nestedMockType, MockStyle nestedMockStyle)
        {
            IMockObject mockObject = CastToMockObject(mock);

            _stubMockStyleDictionary[mockObject, nestedMockType] = nestedMockStyle;
        }
Ejemplo n.º 27
0
		/// <summary>
		/// Defines the receiver.
		/// </summary>
		/// <param name="receiver">The dynamic mock on which the expectation or stub is applied.</param>
		/// <returns>Method syntax defining the method, property or event.</returns>
		private void On(IMockObject receiver)
		{
			MockObject = receiver;

			BuildableExpectation.Receiver = MockObject;
			MockObject.AddExpectation(BuildableExpectation);
		}
Ejemplo n.º 28
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Key"/> class.
 /// </summary>
 /// <param name="mock">The mock object.</param>
 /// <param name="nestedMockType">Type of the nested mock.</param>
 public Key(IMockObject mock, Type nestedMockType)
 {
     Mock           = mock;
     NestedMockType = nestedMockType;
 }
Ejemplo n.º 29
0
		/// <summary>
		/// Adds itself to the <paramref name="result"/> if the <see cref="Receiver"/> matches
		/// the specified <paramref name="mock"/>.
		/// </summary>
		/// <param name="mock">The mock for which expectations are queried.</param>
		/// <param name="result">The result to add matching expectations to.</param>
		public void QueryExpectationsBelongingTo(IMockObject mock, IList<IExpectation> result)
		{
			if (Receiver == mock)
			{
				result.Add(this);
			}
		}
Ejemplo n.º 30
0
 /// <summary>
 /// Adds all expectations to <paramref name="result"/> that are associated to <paramref name="mock"/>.
 /// </summary>
 /// <param name="mock">The mock for which expectations are queried.</param>
 /// <param name="result">The result to add matching expectations to.</param>
 public void QueryExpectationsBelongingTo(IMockObject mock, IList <IExpectation> result)
 {
     ForEach(expectation => expectation.QueryExpectationsBelongingTo(mock, result));
 }