Overloaded. Returns a fake instance of type T, and allows to set the behavior of how the fake object is created.
Namespace: TypeMock.ArrangeActAssert.Fluent
Assembly: Typemock.ArrangeActAssert (in Typemock.ArrangeActAssert.dll) Version: 9.4.1.0 (9.4.1.0)
Syntax
Parameters
- behavior
- Type: TypeMock.ArrangeActAssertMembers
Can be one of the following values:Members Value Description Members.MustSpecifyReturnValues All void calls are ignored. Unless using WhenCalled(Action) on methods that return values and properties, they will throw a . Members.ReturnNulls All void calls are ignored. Unless using WhenCalled(Action) on methods that return values and properties, they will return null values (or zero for value types). Members.CallOriginal All methods are called. Use WhenCalled(Action) to change this behavior. Members.ReturnRecursiveFakes Default. All void calls are ignored. When calling the fake's methods returning values or properties, they will return a fake value, which presents this behavior recursively. Any deep call will never return a Null value.
Type Parameters
- T
- The type of the fake instance to be created.
Return Value
Type: TA fake instance of Type T.
Remarks
Examples
[TestMethod] [Isolated] public void FakeAnInstance_MustSpecifyReturnValues() { RealLogger fake = Isolate.Fake.Instance<RealLogger>(Members.MustSpecifyReturnValues); fake.Count = 0; // This call is faked because we used MustSpecifyReturnValues - count should not increment fake.Increment(); Assert.AreEqual(0, fake.count); // The following statement will throw, because we didn't set behavior on IntCall method. int x = fake.IntCall(); }
[TestMethod] [Isolated] public void FakeAnInstance_UseCallOriginal() { RealLogger fake = Isolate.Fake.Instance<RealLogger>(Members.CallOriginal); fake.Count = 0; // This call is not faked. Count should be incremented. fake.Increment(); Assert.AreEqual(1, fake.Count); }
[TestMethod] [Isolated] public void FakeAnInstance_UseReturnNulls() { RealLogger fake = Isolate.Fake.Instance<RealLogger>(Members.ReturnNulls); fake.Count = 0; // This call is faked because we set faked method behavior using ReturnNulls fake.Increment(); Assert.AreEqual(0, fake.Count); // Since we're returning Null by default the following Assert passes Assert.IsNull(fake.IntCall()); }
[TestMethod] [Isolated] public void RecursiveStubsChain() { RealLogger fake = Isolate.Fake.Instance<RealLogger>(Members.ReturnRecursiveFakes); // This method returns a value type, and therefore, returns a fake 0. Assert.AreEqual(0, fake.ReturnFive()); // This method returns an object, and therefore returns a non-null object Assert.IsNotNull(fake.GetSon()); // And the result of GetSon also returns a non-null object Assert.IsNotNull(fake.GetSon().GetNephew()); }
See Also