Best JustMockLite code snippet using Telerik.JustMock.AutoMock.Ninject.Parameters.Parameter
StandardProvider.cs
Source:StandardProvider.cs
...11using System;12using System.Linq;13using Telerik.JustMock.AutoMock.Ninject.Infrastructure;14using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection;15using Telerik.JustMock.AutoMock.Ninject.Parameters;16using Telerik.JustMock.AutoMock.Ninject.Planning;17using Telerik.JustMock.AutoMock.Ninject.Planning.Directives;18using Telerik.JustMock.AutoMock.Ninject.Planning.Targets;19using Telerik.JustMock.AutoMock.Ninject.Selection;20#endregion21namespace Telerik.JustMock.AutoMock.Ninject.Activation.Providers22{23 using System.Reflection;24 using Telerik.JustMock.AutoMock.Ninject.Selection.Heuristics;25 using Telerik.JustMock.Core;26 /// <summary>27 /// The standard provider for types, which activates instances via a <see cref="IPipeline"/>.28 /// </summary>29 public class StandardProvider : IProvider30 {31 /// <summary>32 /// Gets the type (or prototype) of instances the provider creates.33 /// </summary>34 public Type Type { get; private set; }35 /// <summary>36 /// Gets or sets the planner component.37 /// </summary>38 public IPlanner Planner { get; private set; }39 /// <summary>40 /// Gets or sets the selector component.41 /// </summary>42 public IConstructorScorer ConstructorScorer { get; private set; }43 /// <summary>44 /// Initializes a new instance of the <see cref="StandardProvider"/> class.45 /// </summary>46 /// <param name="type">The type (or prototype) of instances the provider creates.</param>47 /// <param name="planner">The planner component.</param>48 /// <param name="constructorScorer">The constructor scorer component.</param>49 public StandardProvider(Type type, IPlanner planner, IConstructorScorer constructorScorer50 )51 {52 Ensure.ArgumentNotNull(type, "type");53 Ensure.ArgumentNotNull(planner, "planner");54 Ensure.ArgumentNotNull(constructorScorer, "constructorScorer");55 Type = type;56 Planner = planner;57 ConstructorScorer = constructorScorer;58 }59 /// <summary>60 /// Creates an instance within the specified context.61 /// </summary>62 /// <param name="context">The context.</param>63 /// <returns>The created instance.</returns>64 public virtual object Create(IContext context)65 {66 Ensure.ArgumentNotNull(context, "context");67 if (context.Plan == null)68 {69 context.Plan = this.Planner.GetPlan(this.GetImplementationType(context.Request.Service));70 }71 if (!context.Plan.Has<ConstructorInjectionDirective>())72 {73 throw new ActivationException(ExceptionFormatter.NoConstructorsAvailable(context));74 }75 var directives = context.Plan.GetAll<ConstructorInjectionDirective>();76 var bestDirectives = directives77 .GroupBy(option => this.ConstructorScorer.Score(context, option))78 .OrderByDescending(g => g.Key)79 .First();80 if (bestDirectives.Skip(1).Any())81 {82 throw new ActivationException(ExceptionFormatter.ConstructorsAmbiguous(context, bestDirectives));83 }84 var directive = bestDirectives.Single();85 var arguments = directive.Targets.Select(target => this.GetValue(context, target)).ToArray();86 var injector = directive.Injector;87 return ProfilerInterceptor.GuardExternal(() => injector(arguments));88 }89 /// <summary>90 /// Gets the value to inject into the specified target.91 /// </summary>92 /// <param name="context">The context.</param>93 /// <param name="target">The target.</param>94 /// <returns>The value to inject into the specified target.</returns>95 public object GetValue(IContext context, ITarget target)96 {97 Ensure.ArgumentNotNull(context, "context");98 Ensure.ArgumentNotNull(target, "target");99 var parameter = context100 .Parameters.OfType<IConstructorArgument>()101 .Where(p => p.AppliesToTarget(context, target)).SingleOrDefault();102 return parameter != null ? parameter.GetValue(context, target) : target.ResolveWithin(context);103 }104 /// <summary>105 /// Gets the implementation type that the provider will activate an instance of106 /// for the specified service.107 /// </summary>108 /// <param name="service">The service in question.</param>109 /// <returns>The implementation type that will be activated.</returns>110 public Type GetImplementationType(Type service)111 {112 Ensure.ArgumentNotNull(service, "service");113 return Type.ContainsGenericParameters ? Type.MakeGenericType(service.GetGenericArguments()) : Type;114 }115 /// <summary>116 /// Gets a callback that creates an instance of the <see cref="StandardProvider"/>117 /// for the specified type.118 /// </summary>119 /// <param name="prototype">The prototype the provider instance will create.</param>120 /// <returns>The created callback.</returns>121 public static Func<IContext, IProvider> GetCreationCallback(Type prototype)122 {123 Ensure.ArgumentNotNull(prototype, "prototype");124 return ctx => new StandardProvider(prototype, ctx.Kernel.Components.Get<IPlanner>(), ctx.Kernel.Components.Get<ISelector>().ConstructorScorer);125 }126 /// <summary>127 /// Gets a callback that creates an instance of the <see cref="StandardProvider"/>...
StandardConstructorScorer.cs
Source:StandardConstructorScorer.cs
...28 using Telerik.JustMock.AutoMock.Ninject.Activation;29 using Telerik.JustMock.AutoMock.Ninject.Components;30 using Telerik.JustMock.AutoMock.Ninject.Infrastructure;31 using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language;32 using Telerik.JustMock.AutoMock.Ninject.Parameters;33 using Telerik.JustMock.AutoMock.Ninject.Planning.Directives;34 using Telerik.JustMock.AutoMock.Ninject.Planning.Targets;35 /// <summary>36 /// Scores constructors by either looking for the existence of an injection marker37 /// attribute, or by counting the number of parameters.38 /// </summary>39 public class StandardConstructorScorer : NinjectComponent, IConstructorScorer40 {41 /// <summary>42 /// Gets the score for the specified constructor.43 /// </summary>44 /// <param name="context">The injection context.</param>45 /// <param name="directive">The constructor.</param>46 /// <returns>The constructor's score.</returns>47 public virtual int Score(IContext context, ConstructorInjectionDirective directive)48 {49 Ensure.ArgumentNotNull(context, "context");50 Ensure.ArgumentNotNull(directive, "constructor");51 if (directive.Constructor.HasAttribute(Settings.InjectAttribute))52 {53 return int.MaxValue;54 }55 var score = 1;56 foreach (ITarget target in directive.Targets)57 {58 if (ParameterExists(context, target))59 {60 score++;61 continue;62 }63 64 if (BindingExists(context, target))65 {66 score++;67 continue;68 }69 score++;70 if (score > 0)71 {72 score += int.MinValue;73 }74 }75 76 return score;77 }78 /// <summary>79 /// Checkes whether a binding exists for a given target.80 /// </summary>81 /// <param name="context">The context.</param>82 /// <param name="target">The target.</param>83 /// <returns>Whether a binding exists for the target in the given context.</returns>84 protected virtual bool BindingExists(IContext context, ITarget target)85 {86 return this.BindingExists(context.Kernel, context, target);87 }88 /// <summary>89 /// Checkes whether a binding exists for a given target on the specified kernel.90 /// </summary>91 /// <param name="kernel">The kernel.</param>92 /// <param name="context">The context.</param>93 /// <param name="target">The target.</param>94 /// <returns>Whether a binding exists for the target in the given context.</returns>95 protected virtual bool BindingExists(IKernel kernel, IContext context, ITarget target)96 {97 var targetType = GetTargetType(target);98 return kernel.GetBindings(targetType).Any(b => !b.IsImplicit)99 || target.HasDefaultValue;100 }101 private Type GetTargetType(ITarget target)102 {103 var targetType = target.Type;104 if (targetType.IsArray)105 {106 targetType = targetType.GetElementType();107 }108 if (targetType.IsGenericType && targetType.GetInterfaces().Any(type => type == typeof(IEnumerable)))109 {110 targetType = targetType.GetGenericArguments()[0];111 }112 return targetType;113 }114 /// <summary>115 /// Checks whether any parameters exist for the geiven target..116 /// </summary>117 /// <param name="context">The context.</param>118 /// <param name="target">The target.</param>119 /// <returns>Whether a parameter exists for the target in the given context.</returns>120 protected virtual bool ParameterExists(IContext context, ITarget target)121 {122 return context123 .Parameters.OfType<IConstructorArgument>()124 .Any(parameter => parameter.AppliesToTarget(context, target));125 }126 }127}...
PropertyInjectionStrategy.cs
Source:PropertyInjectionStrategy.cs
...15using Telerik.JustMock.AutoMock.Ninject.Infrastructure;16using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection;17using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language;18using Telerik.JustMock.AutoMock.Ninject.Injection;19using Telerik.JustMock.AutoMock.Ninject.Parameters;20using Telerik.JustMock.AutoMock.Ninject.Planning.Directives;21using Telerik.JustMock.AutoMock.Ninject.Planning.Targets;22#endregion23namespace Telerik.JustMock.AutoMock.Ninject.Activation.Strategies24{25 /// <summary>26 /// Injects properties on an instance during activation.27 /// </summary>28 public class PropertyInjectionStrategy : ActivationStrategy29 {30 private const BindingFlags DefaultFlags = BindingFlags.Public | BindingFlags.Instance;31 private BindingFlags Flags32 {33 get34 {35 #if !NO_LCG && !SILVERLIGHT36 return Settings.InjectNonPublic ? (DefaultFlags | BindingFlags.NonPublic) : DefaultFlags;37 #else38 return DefaultFlags;39 #endif40 }41 }42 /// <summary>43 /// Gets the injector factory component.44 /// </summary>45 public IInjectorFactory InjectorFactory { get; set; }46 /// <summary>47 /// Initializes a new instance of the <see cref="PropertyInjectionStrategy"/> class.48 /// </summary>49 /// <param name="injectorFactory">The injector factory component.</param>50 public PropertyInjectionStrategy(IInjectorFactory injectorFactory)51 {52 this.InjectorFactory = injectorFactory;53 }54 /// <summary>55 /// Injects values into the properties as described by <see cref="PropertyInjectionDirective"/>s56 /// contained in the plan.57 /// </summary>58 /// <param name="context">The context.</param>59 /// <param name="reference">A reference to the instance being activated.</param>60 public override void Activate(IContext context, InstanceReference reference)61 {62 Ensure.ArgumentNotNull(context, "context");63 Ensure.ArgumentNotNull(reference, "reference");64 var propertyValues = context.Parameters.OfType<IPropertyValue>().ToList();65 foreach (var directive in context.Plan.GetAll<PropertyInjectionDirective>())66 {67 object value = this.GetValue(context, directive.Target, propertyValues);68 directive.Injector(reference.Instance, value);69 }70 this.AssignProperyOverrides(context, reference, propertyValues);71 }72 /// <summary>73 /// Applies user supplied override values to instance properties.74 /// </summary>75 /// <param name="context">The context.</param>76 /// <param name="reference">A reference to the instance being activated.</param>77 /// <param name="propertyValues">The parameter override value accessors.</param>78 private void AssignProperyOverrides(IContext context, InstanceReference reference, IList<IPropertyValue> propertyValues)...
Parameter
Using AI Code Generation
1using Telerik.JustMock.AutoMock.Ninject.Parameters;2{3 {4 void DoSomething();5 }6 {7 public void DoSomething()8 {9 }10 }11 {12 public void Test()13 {14 var mock = Mock.Create<MyClass>();15 var parameter = new Parameter<IMyInterface>(mock);16 var kernel = new StandardKernel();17 kernel.Bind<MyClass>().ToSelf().WithConstructorArgument(parameter);18 var myClass = kernel.Get<MyClass>();19 Assert.Same(myClass, mock);20 }21 }22}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!