/// <summary> /// Creates a new specimen based on a request. /// </summary> /// <param name="request">The request that describes what to create.</param> /// <param name="context">A context that can be used to create other specimens.</param> /// <returns> /// A dynamic mock instance of the requested interface or abstract class if possible; /// otherwise a <see cref="NoSpecimen"/> instance. /// </returns> public object Create(object request, ISpecimenContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (!this.mockableSpecification.IsSatisfiedBy(request)) #pragma warning disable 618 { return(new NoSpecimen(request)); } #pragma warning restore 618 var t = request as Type; if (t == null) #pragma warning disable 618 { return(new NoSpecimen(request)); } #pragma warning restore 618 var result = MockRelay.ResolveMock(t, context); // Note: null is a valid specimen (e.g., returned by NullRecursionHandler) if (result is NoSpecimen || result is OmitSpecimen || result == null) { return(result); } var m = result as Mock; if (m == null) #pragma warning disable 618 { return(new NoSpecimen(request)); } #pragma warning restore 618 return(m.Object); }
/// <summary> /// Creates a new specimen based on a request. /// </summary> /// <param name="request">The request that describes what to create.</param> /// <param name="context">A context that can be used to create other specimens.</param> /// <returns> /// A dynamic mock instance of the requested interface or abstract class if possible; /// otherwise a <see cref="NoSpecimen"/> instance. /// </returns> public object Create(object request, ISpecimenContext context) { if (context == null) { throw new ArgumentNullException("context"); } var t = request as Type; if (!this.shouldBeMocked(t)) { return(new NoSpecimen(request)); } var m = MockRelay.ResolveMock(t, context); if (m == null) { return(new NoSpecimen(request)); } return(m.Object); }