public virtual void SetPropertyValues() { var instance = Fixture.Create<PocoClass>(); var objectModel = CreateObjectModel(instance); var properties = objectModel.GetProperties().ToList(); if (properties.Any()) { foreach (var property in properties) { if (property.ReadOnly) continue; var type = property.Type ?? typeof (object); var specimenContext = new SpecimenContext(Fixture); var newValue = specimenContext.Resolve(type); property.SetValue(newValue); Assert.AreEqual(newValue, property.Value); } } else { Assert.Inconclusive("ObjectModel has no properties, cannot test if values are set"); } }
public void CombineExplictPropertyWithAutoProperties() { // Fixture setup var expectedText = "Fnaah"; var specifiedCommand = new BindingCommand<DoublePropertyHolder<string, int>, string>(ph => ph.Property1, expectedText); var reservedProperty = new InverseRequestSpecification(specifiedCommand); var customizedBuilder = new Postprocessor<DoublePropertyHolder<string, int>>( new Postprocessor<DoublePropertyHolder<string, int>>( new MethodInvoker(new ModestConstructorQuery()), specifiedCommand), new AutoPropertiesCommand<DoublePropertyHolder<string, int>>(reservedProperty), new AnyTypeSpecification()); var builder = new CompositeSpecimenBuilder( customizedBuilder, Scenario.CreateAutoPropertyBuilder()); var container = new SpecimenContext(builder); // Exercise system var result = container.Resolve(typeof(DoublePropertyHolder<string, int>)); // Verify outcome var actual = Assert.IsAssignableFrom<DoublePropertyHolder<string, int>>(result); Assert.Equal(expectedText, actual.Property1); Assert.Equal(1, actual.Property2); // Teardown }
/// <inerhitdoc /> public object GenerateAnonymousValue(AnonymousValueFixture any, Type type, string propertyName) { // http://autofixture.codeplex.com/workitem/4229 var context = new SpecimenContext(any.Fixture); var specimen = context.Resolve(type); return specimen; }
public void CreateWillReturnCorrectResult() { // Fixture setup var expectedResult = new object(); var builder = new DelegatingSpecimenBuilder { OnCreate = (r, c) => expectedResult }; var sut = new SpecimenContext(builder); // Exercise system var dummyRequest = new object(); var result = sut.Resolve(dummyRequest); // Verify outcome Assert.Equal(expectedResult, result); // Teardown }
private TestCaseParameters GetParametersForMethod(IMethodInfo method) { try { var specimenBuilder = new SpecimenContext(this._fixture); var parameterValues = method.GetParameters() .Select(p => specimenBuilder.Resolve(p.ParameterInfo)) .ToArray(); return new TestCaseParameters(parameterValues); } catch (Exception ex) { return new TestCaseParameters(ex); } }
public void CreateWillInvokeBuilderWithCorrectRequest() { // Fixture setup var expectedRequest = new object(); var mockVerified = false; var builderMock = new DelegatingSpecimenBuilder(); builderMock.OnCreate = (r, c) => { Assert.Equal(expectedRequest, r); mockVerified = true; return new object(); }; var sut = new SpecimenContext(builderMock); // Exercise system sut.Resolve(expectedRequest); // Verify outcome Assert.True(mockVerified, "Mock verification"); // Teardown }
public void CreateDoubleMixedParameterizedTypeWithNumberBasedStringGenerator() { // Fixture setup var intGenerator = new Int32SequenceGenerator(); var builder = new CompositeSpecimenBuilder( intGenerator, new StringGenerator(() => intGenerator.CreateAnonymous()), new Int64SequenceGenerator(), new DecimalSequenceGenerator(), new BooleanSwitch(), new GuidGenerator(), new MethodInvoker(new ModestConstructorQuery()), new ParameterRequestRelay(), new StringSeedRelay(), new SeedIgnoringRelay()); var container = new SpecimenContext(builder); // Exercise system var result = (TripleParameterType<int, string, int>)container.Resolve(typeof(TripleParameterType<int, string, int>)); // Verify outcome Assert.Equal(1, result.Parameter1); Assert.Equal("parameter22", result.Parameter2); Assert.Equal(3, result.Parameter3); // Teardown }
public void CreateAndAddProperyValues() { // Fixture setup var ctorInvoker = new MethodInvoker(new ModestConstructorQuery()); var strCmd = new BindingCommand<DoublePropertyHolder<string, int>, string>(ph => ph.Property1); var intCmd = new BindingCommand<DoublePropertyHolder<string, int>, int>(ph => ph.Property2); var strPostprocessor = new Postprocessor<DoublePropertyHolder<string, int>>(ctorInvoker, strCmd); var intPostprocessor = new Postprocessor<DoublePropertyHolder<string, int>>(strPostprocessor, intCmd); var builder = new CompositeSpecimenBuilder( new FilteringSpecimenBuilder(intPostprocessor, new ExactTypeSpecification(typeof(DoublePropertyHolder<string, int>))), Scenario.CreateAutoPropertyBuilder()); var container = new SpecimenContext(builder); // Exercise system var result = container.Resolve(typeof(DoublePropertyHolder<string, int>)); // Verify outcome var actual = Assert.IsAssignableFrom<DoublePropertyHolder<string, int>>(result); Assert.False(string.IsNullOrEmpty(actual.Property1), "Property1"); Assert.Equal(1, actual.Property2); // Teardown }
private object Resolve(ParameterInfo p) { var context = new SpecimenContext(this.Fixture); return context.Resolve(p); }
private ISpecimenBuilder FreezeTargetType(IFixture fixture) { var context = new SpecimenContext(fixture); var specimen = context.Resolve(this.targetType); return new FixedBuilder(specimen); }
public void FixtureDoesNotHijackCollectionInterfacesIfAnotherCustomizationExistsForThem(Type collectionInterface) { // Fixture setup var fixture = new Fixture().Customize(new MultipleCustomization()).Customize(new AutoNSubstituteCustomization()); var context = new SpecimenContext(fixture.Compose()); // Exercise system var result = context.Resolve(new SeededRequest(collectionInterface, null)); // Verify outcome Assert.NotEmpty((IEnumerable)result); }
public void CustomizeWithCompetingSpecimenBuilderForTheSameTypeShouldReturnTheFrozenSpecimen() { // Fixture setup var fixture = new Fixture(); var context = new SpecimenContext(fixture); var frozenType = typeof(object); var competingBuilder = new DelegatingSpecimenBuilder { OnCreate = (request, ctx) => request.Equals(frozenType) ? new object() #pragma warning disable 618 : new NoSpecimen(request) #pragma warning restore 618 }; var sut = new FreezeOnMatchCustomization( frozenType, new ExactTypeSpecification(frozenType)); // Exercise system fixture.Customizations.Add(competingBuilder); sut.Customize(fixture); // Verify outcome Assert.Equal(context.Resolve(frozenType), context.Resolve(frozenType)); }
public void FreezeByMatchingImplementedInterfacesShouldReturnTheRightSpecimen( Type frozenType, Type requestedType) { // Fixture setup var fixture = new Fixture(); var context = new SpecimenContext(fixture); var sut = new FreezeOnMatchCustomization( frozenType, new ImplementedInterfaceSpecification(frozenType)); // Exercise system sut.Customize(fixture); // Verify outcome var frozen = context.Resolve(frozenType); var requested = context.Resolve(requestedType); Assert.Same(frozen, requested); // Teardown }
public void FreezeByMatchingBaseTypeShouldReturnTheRightSpecimen( Type frozenType, Type requestedType, bool areEqual) { // Fixture setup var fixture = new Fixture(); var context = new SpecimenContext(fixture); var sut = new FreezeOnMatchCustomization( frozenType, new DirectBaseTypeSpecification(frozenType)); // Exercise system sut.Customize(fixture); // Verify outcome var frozen = context.Resolve(frozenType); var requested = context.Resolve(requestedType); Assert.Equal(areEqual, frozen.Equals(requested)); // Teardown }
private object CustomizeAndCreate(IFixture fixture, ParameterInfo p) { var customizations = p.GetCustomAttributes(typeof(CustomizeAttribute), false) .OfType<CustomizeAttribute>() .Select(attr => attr.GetCustomization(p)); foreach (var c in customizations) { fixture.Customize(c); } var context = new SpecimenContext(fixture); return context.Resolve(p); }