本文整理汇总了C#中ISpecimenContext.Resolve方法的典型用法代码示例。如果您正苦于以下问题:C# ISpecimenContext.Resolve方法的具体用法?C# ISpecimenContext.Resolve怎么用?C# ISpecimenContext.Resolve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ISpecimenContext
的用法示例。
在下文中一共展示了ISpecimenContext.Resolve方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
/// <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>
/// The requested specimen if possible; otherwise a <see cref="NoSpecimen"/> instance.
/// </returns>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (!typeof(Uri).Equals(request))
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var scheme = context.Resolve(typeof(UriScheme)) as UriScheme;
if (scheme == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var authority = context.Resolve(typeof(string)) as string;
if (authority == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
return UriGenerator.CreateAnonymous(scheme, authority);
}
示例2: Create
public object Create(object request, ISpecimenContext context)
{
if (typeof(Point).Equals(request))
{
var x = (int)context.Resolve(typeof(int));
var y = (int)context.Resolve(typeof(int));
return new Point(x, y);
}
return new NoSpecimen(request);
}
示例3: TryCreateMailAddress
private static object TryCreateMailAddress(object request, ISpecimenContext context)
{
var localPart = context.Resolve(typeof(EmailAddressLocalPart)) as EmailAddressLocalPart;
var domainName = context.Resolve(typeof(DomainName)) as DomainName;
if (localPart == null || domainName == null)
{
return new NoSpecimen(request);
}
var email = string.Format(CultureInfo.InvariantCulture, "{0} <{0}@{1}>", localPart, domainName);
return new MailAddress(email);
}
示例4: AddMany
public static void AddMany(object specimen, ISpecimenContext context)
{
if (specimen == null)
{
throw new ArgumentNullException("specimen");
}
if (context == null)
{
throw new ArgumentNullException("context");
}
var typeArguments = specimen.GetType().GetGenericArguments();
if (typeArguments.Length != 2)
{
throw new ArgumentException("The specimen must be an instance of IDictionary<TKey, TValue>.", "specimen");
}
if (!typeof(IDictionary<,>).MakeGenericType(typeArguments).IsAssignableFrom(specimen.GetType()))
{
throw new ArgumentException("The specimen must be an instance of IDictionary<TKey, TValue>.", "specimen");
}
var kvpType = typeof(KeyValuePair<,>).MakeGenericType(typeArguments);
var enumerable = context.Resolve(new MultipleRequest(kvpType)) as IEnumerable;
foreach (var item in enumerable)
{
var addMethod = typeof(ICollection<>).MakeGenericType(kvpType).GetMethod("Add", new[] { kvpType });
addMethod.Invoke(specimen, new[] { item });
}
}
示例5: Create
/// <summary>
/// Creates a relayed request based on the <see cref="SubstituteAttribute"/> applied to a code element and
/// resolves it from the given <paramref name="context"/>.
/// </summary>
/// <returns>
/// A specimen resolved from the <paramref name="context"/> based on a relayed request.
/// If the <paramref name="request"/> code element does not have <see cref="SubstituteAttribute"/> applied,
/// returns <see cref="NoSpecimen"/>.
/// </returns>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var customAttributeProvider = request as ICustomAttributeProvider;
if (customAttributeProvider == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var attribute = customAttributeProvider.GetCustomAttributes(typeof(SubstituteAttribute), true)
.OfType<SubstituteAttribute>().FirstOrDefault();
if (attribute == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
object substituteRequest = CreateSubstituteRequest(customAttributeProvider, attribute);
return context.Resolve(substituteRequest);
}
示例6: Create
public object Create(object request, ISpecimenContext context)
{
var customAttributeProvider = request as ICustomAttributeProvider;
if (customAttributeProvider == null)
{
return new NoSpecimen(request);
}
var attribute =
customAttributeProvider.GetCustomAttributes(typeof(ContentAttribute), true)
.OfType<ContentAttribute>()
.FirstOrDefault();
if (attribute == null)
{
return new NoSpecimen(request);
}
var parameterInfo = request as ParameterInfo;
if (parameterInfo == null)
{
return new NoSpecimen(request);
}
return context.Resolve(parameterInfo.ParameterType);
}
示例7: Create
/// <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>
/// The requested specimen if possible; otherwise a <see cref="NoSpecimen"/> instance.
/// </returns>
public object Create(object request, ISpecimenContext context)
{
if (request == null)
{
return new NoSpecimen();
}
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var customAttributeProvider = request as ICustomAttributeProvider;
if (customAttributeProvider == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var regularExpressionAttribute = customAttributeProvider.GetCustomAttributes(typeof(RegularExpressionAttribute), inherit: true).Cast<RegularExpressionAttribute>().SingleOrDefault();
if (regularExpressionAttribute == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
return context.Resolve(new RegularExpressionRequest(regularExpressionAttribute.Pattern));
}
示例8: Create
/// <summary>
/// Creates an anonymous string based on a seed.
/// </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 string with the seed prefixed to a string created by <paramref name="context"/> if
/// possible; otherwise, <see langword="null"/>.
/// </returns>
/// <remarks>
/// <para>
/// This method only returns an instance if a number of conditions are satisfied.
/// <paramref name="request"/> must represent a request for a seed string, and
/// <paramref name="context"/> must be able to create a string.
/// </para>
/// </remarks>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var seededRequest = request as SeededRequest;
if (seededRequest == null ||
seededRequest.Request != typeof(string))
{
return new NoSpecimen(request);
}
var seed = seededRequest.Seed as string;
if (seed == null)
{
return new NoSpecimen(request);
}
var containerResult = context.Resolve(typeof(string));
if (containerResult is NoSpecimen)
{
return containerResult;
}
return seed + containerResult;
}
示例9: Create
/// <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.fakeableSpecification.IsSatisfiedBy(request))
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
var type = request as Type;
if (type == null)
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
var fakeType = typeof(Fake<>).MakeGenericType(type);
var fake = context.Resolve(fakeType);
if (!fakeType.IsInstanceOfType(fake))
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
return fake.GetType().GetProperty("FakedObject").GetValue(fake, null);
}
示例10: Create
/// <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>
/// The requested specimen if possible; otherwise a <see cref="NoSpecimen"/> instance.
/// </returns>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (request == null || !typeof(EmailAddressLocalPart).Equals(request))
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var localPart = context.Resolve(typeof(string)) as string;
if (string.IsNullOrEmpty(localPart))
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
return new EmailAddressLocalPart(localPart);
}
示例11: Create
public object Create(object request, ISpecimenContext context)
{
var result = context.Resolve(request);
if (result == null || !typeof(IViewModel).IsAssignableFrom(result.GetType()))
{
return result;
}
var viewModel = (IViewModel)result;
var viewModelRequestAsObject = context.Resolve(typeof(INavigationRequest));
if (viewModelRequestAsObject.GetType() != typeof(INavigationRequest))
{
throw new InvalidOperationException("Could not intialized the view model because the navigation request could not be created.");
}
viewModel.Initialize((INavigationRequest)viewModelRequestAsObject);
return viewModel;
}
示例12: Create
public object Create(object request, ISpecimenContext context)
{
var specimen = _postprocessor.Create(request, context);
if (specimen.GetType() == typeof (NoSpecimen))
{
return specimen;
}
var mockedType = GetMockedType(specimen);
var methodsWithTasks = GetMethodsWithTasks(mockedType);
var isAny = typeof (It).GetMethod("IsAny");
foreach (var method in methodsWithTasks)
{
var mockSetupMethod = GetMockSetupMethod(mockedType, method.ReturnType);
var mockReturnMethod = GetMockReturnsMethod(mockedType, method.ReturnType);
var returnTaskType = GetReturnType(method.ReturnType);
var returnValue = context.Resolve(returnTaskType);
var returnTask = typeof (Task).GetMethod("FromResult")
.MakeGenericMethod(returnTaskType)
.Invoke(null, new object[] {returnValue});
var parameters = method.GetParameters()
.Select(info =>(Expression) Expression.Constant(isAny.MakeGenericMethod(info.ParameterType).Invoke(null, new object[] {})))
.ToArray();
var parameter = Expression.Parameter(mockedType);
var body = Expression.Call(parameter, method, parameters);
var lambda = Expression.Lambda(body, parameter);
var setup = mockSetupMethod.Invoke(specimen, new object[] {lambda});
mockReturnMethod.Invoke(setup, new object[] {returnTask});
}
return specimen;
}
示例13: Create
/// <summary>
/// Creates a substitute when request is an abstract type.
/// </summary>
/// <returns>
/// A substitute resolved from the <paramref name="context"/> when <paramref name="request"/> is an abstract
/// type or <see cref="NoSpecimen"/> for all other requests.
/// </returns>
/// <exception cref="InvalidOperationException">
/// An attempt to resolve a substitute from the <paramref name="context"/> returned an object that was not
/// created by NSubstitute.
/// </exception>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var requestedType = request as Type;
if (requestedType == null || !requestedType.IsAbstract)
{
return new NoSpecimen(request);
}
object substitute = context.Resolve(new SubstituteRequest(requestedType));
try
{
SubstitutionContext.Current.GetCallRouterFor(substitute);
}
catch (NotASubstituteException e)
{
throw new InvalidOperationException(
string.Format(
CultureInfo.CurrentCulture,
"Object resolved by request for substitute of {0} was not created by NSubstitute. " +
"Ensure that {1} was added to Fixture.Customizations.",
requestedType.FullName, typeof(SubstituteRequestHandler).FullName),
e);
}
return substitute;
}
示例14: Create
/// <summary>
/// Creates a new specimen based on a requested range.
/// </summary>
/// <param name="request">The request that describes what to create.</param>
/// <param name="context">A container that can be used to create other specimens.</param>
/// <returns>
/// A specimen created from a <see cref="RangedNumberRequest"/> encapsulating the operand
/// type, the minimum and the maximum of the requested number, if possible; otherwise,
/// a <see cref="NoSpecimen"/> instance.
/// </returns>
public object Create(object request, ISpecimenContext context)
{
if (request == null)
{
return new NoSpecimen();
}
if (context == null)
{
throw new ArgumentNullException("context");
}
var customAttributeProvider = request as ICustomAttributeProvider;
if (customAttributeProvider == null)
{
return new NoSpecimen(request);
}
var rangeAttribute = customAttributeProvider.GetCustomAttributes(typeof(RangeAttribute), inherit: true).Cast<RangeAttribute>().SingleOrDefault();
if (rangeAttribute == null)
{
return new NoSpecimen(request);
}
return context.Resolve(RangeAttributeRelay.Create(rangeAttribute, request));
}
示例15: Create
/// <summary>
/// Creates an anonymous string based on a seed.
/// </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 string with the seed prefixed to a string created by <paramref name="context"/> if
/// possible; otherwise, <see langword="null"/>.
/// </returns>
/// <remarks>
/// <para>
/// This method only returns an instance if a number of conditions are satisfied.
/// <paramref name="request"/> must represent a request for a seed string, and
/// <paramref name="context"/> must be able to create a string.
/// </para>
/// </remarks>
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var seededRequest = request as SeededRequest;
if (seededRequest == null ||
(!seededRequest.Request.Equals(typeof(string))))
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var seed = seededRequest.Seed as string;
if (seed == null)
{
#pragma warning disable 618
return new NoSpecimen(request);
#pragma warning restore 618
}
var containerResult = context.Resolve(typeof(string));
if (containerResult is NoSpecimen)
{
return containerResult;
}
return seed + containerResult;
}