/// <summary> /// Configures the specified fixture's act step to be aN HTTP request with the specified method, url and body. /// </summary> /// <param name="fixture">The fixture.</param> /// <param name="method">The method.</param> /// <param name="url">The URL.</param> /// <param name="body">The body.</param> /// <returns></returns> public static IMvcFunctionalTestFixture WhenCallingRestMethod(this IMvcFunctionalTestFixture fixture, HttpMethod method, string url, object body = null) { var content = body == null ? null : new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); fixture.When(method, url, content); return(fixture); }
/// <summary> /// Configures the specified fixture's act step to be aN HTTP request with the specified method, url and body. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="method">The method.</param> /// <param name="url">The URL.</param> /// <param name="model">The model.</param> /// <returns></returns> public static IMvcFunctionalTestFixture WhenCallingRestMethod <TModel>(this IMvcFunctionalTestFixture fixture, HttpMethod method, string url, out TModel model) { model = fixture.Create <TModel>(); return(fixture.WhenCallingRestMethod(method, url, model)); }
/// <summary> /// Uses the faker on the fixture as a factory for some fake data. /// </summary> /// <typeparam name="TFake">The type of the fake.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="factory">The factory.</param> /// <param name="fake">The fake.</param> /// <returns></returns> public static IMvcFunctionalTestFixture HavingFake <TFake>(this IMvcFunctionalTestFixture fixture, Func <Faker, TFake> factory, out TFake fake) { fake = factory(fixture.Faker); return(fixture); }
/// <summary> /// Configures AutoFixture. /// </summary> /// <param name="fixture">The fixture.</param> /// <param name="action">The action.</param> /// <returns></returns> public static IMvcFunctionalTestFixture HavingAutoFixture( this IMvcFunctionalTestFixture fixture, Action <IFixture> action) { action(fixture.AutoFixture); return(fixture); }
/// <summary> /// Configures the specified fixture's act step to be an HTTP request with the specified method, url and body. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="method">The method.</param> /// <param name="url">The URL.</param> /// <param name="model">The model.</param> /// <param name="configurator">The configurator.</param> /// <returns></returns> public static IMvcFunctionalTestFixture WhenCallingRestMethod <TModel>(this IMvcFunctionalTestFixture fixture, HttpMethod method, string url, out TModel model, Action <Faker, TModel> configurator = null) => fixture.HavingModel(out model, configurator) .WhenCallingRestMethod(method, url, model);
/// <summary> /// Configures a MySQL database with the specified context type. /// This will create a bootstrap job that will drop and recreate the MySQL database. /// The connection string is modified to create a database named after the current executing test. /// I.e. tests can run in parallel. /// For this to work, the connection string must have root permissions i.e. be able to drop and recreate any database. /// We assume that this super user connection string can be found in the FunctionalTests environment. /// </summary> /// <typeparam name="TDbContext">The type of the database context.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="bootstrap">The bootstrap.</param> /// <param name="environment">The environment.</param> /// <returns></returns> public static IMvcFunctionalTestFixture HavingMySqlDatabase <TDbContext>(this IMvcFunctionalTestFixture fixture, Action <IMySqlBootstrapDataContext> bootstrap = null, string environment = "FunctionalTests") where TDbContext : DbContext { var testData = new MySqlBootstrapDataContext(); bootstrap?.Invoke(testData); return(fixture.HavingAspNetEnvironment(environment) .HavingBootstrap <MySqlBootstrap <TDbContext> >() .HavingServices(services => services.AddSingleton(testData)) .HavingConfiguration((output, builder) => { var testName = output.GetCurrentTestName(); // We cannot use the test name directly for the database name as it may be longer than the maximum allowed 64 characters. var testNameHash = new Guid(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(testName))); output.WriteLine("Using test database: " + testNameHash); var connectionString = new MySqlConnectionStringBuilder(builder.Build().GetConnectionString("mysql")) { Database = testNameHash.ToString() }.ToString(); var key = ConfigurationPath.Combine("ConnectionStrings", "mysql"); var memoryConfig = new Dictionary <string, string> { [key] = connectionString }; builder.AddInMemoryCollection(memoryConfig); })); }
/// <summary> /// Configures the fixture perform the specified HTTP action. /// </summary> /// <param name="fixture">The fixture.</param> /// <param name="method">The HTTP method.</param> /// <param name="uri">The URI.</param> /// <param name="content">The HTTP content.</param> public static IMvcFunctionalTestFixture When(this IMvcFunctionalTestFixture fixture, HttpMethod method, string uri, HttpContent content) => fixture.When(message => { message.Method = method; message.RequestUri = new Uri(uri, UriKind.Relative); message.Content = content; });
/// <summary> /// Adds an assertion to the specified fixture that the JSON result will be equivalent to the specified model. /// </summary> /// <typeparam name="TResponseModel">The type of the response model.</typeparam> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="models">The models.</param> /// <param name="options">The options.</param> /// <returns></returns> public static IMvcFunctionalTestFixture ShouldReturnJsonCollectionContainingEquivalentModels <TResponseModel, TModel>( this IMvcFunctionalTestFixture fixture, ICollection <TModel> models, Func <EquivalencyAssertionOptions <TModel>, EquivalencyAssertionOptions <TModel> > options = null) => fixture.ShouldReturnJson(models.Select <TModel, Action <ICollection <TResponseModel> > >(e => x => x.Should().ContainEquivalentOf(e, options ?? (c => c))) .ToArray());
/// <summary> /// Gets the property of the specified type with the specified key and runs the specified property action. /// </summary> /// <typeparam name="TProperty">The type of the property.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="key">The key.</param> /// <param name="propertyAction">The property action.</param> /// <returns></returns> public static IMvcFunctionalTestFixture HavingConfiguredProperty <TProperty>( this IMvcFunctionalTestFixture fixture, string key, Action <TProperty> propertyAction) => fixture .HavingConfiguredProperty <TProperty>(key, out var property) .Having(() => propertyAction(property));
/// <summary> /// Picks a random model from the specified collection and optionally mutates it. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="models">The models.</param> /// <param name="model">The model.</param> /// <param name="mutationFunc">The mutation function.</param> /// <returns></returns> public static IMvcFunctionalTestFixture HavingRandom <TModel>(this IMvcFunctionalTestFixture fixture, ICollection <TModel> models, out TModel model, Action <TModel> mutationFunc = null) { model = models.PickRandom(); mutationFunc?.Invoke(model); return(fixture); }
protected override IMvcFunctionalTestFixture GivenSetup(IMvcFunctionalTestFixture fixture) => fixture .HavingFake(f => f.Date.Timespan(), out var futureOffset) .HavingProperty(FutureOffset, futureOffset) .HavingConfiguration(b => b.AddInMemoryCollection(new Dictionary <string, string> { [FutureOffset] = futureOffset.ToString() }));
/// <summary> /// Creates an auto fixture constructed instance of the specified model. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="model">The model.</param> /// <param name="configurator">The configurator.</param> /// <returns></returns> public static IMvcFunctionalTestFixture HavingModel <TModel>(this IMvcFunctionalTestFixture fixture, out TModel model, Action <TModel> configurator = null) { model = fixture.Create <TModel>(); configurator?.Invoke(model); return(fixture); }
/// <summary> /// Gets the property of the specified type with the specified key. /// </summary> /// <typeparam name="TProperty">The type of the property.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="key">The key.</param> /// <param name="property">The property.</param> /// <returns></returns> public static IMvcFunctionalTestFixture HavingConfiguredProperty <TProperty>( this IMvcFunctionalTestFixture fixture, string key, out TProperty property) { property = fixture.GetProperty <TProperty>(key); return(fixture); }
/// <summary> /// Picks a random model from the specified collection and optionally mutates it. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="models">The models.</param> /// <param name="model">The model.</param> /// <param name="mutationFunc">The mutation function.</param> /// <returns></returns> public static IMvcFunctionalTestFixture HavingRandom <TModel>(this IMvcFunctionalTestFixture fixture, ICollection <TModel> models, out TModel model, Action <TModel> mutationFunc = null) { model = fixture.Faker.Random.CollectionItem(models); mutationFunc?.Invoke(model); return(fixture); }
/// <summary> /// Runs the specified fixture action if a property exists in the fixture that matches the specified value predicate. /// </summary> /// <param name="fixture">The fixture.</param> /// <param name="key">The key.</param> /// <param name="valuePredicate">The value predicate.</param> /// <param name="action">The action.</param> /// <returns></returns> public static IMvcFunctionalTestFixture WhenHavingStringProperty(this IMvcFunctionalTestFixture fixture, string key, Func <string, bool> valuePredicate, Action <IMvcFunctionalTestFixture> action) { if (fixture.Properties.TryGetValue(key, out var value) && value is string s && valuePredicate(s)) { action(fixture); } return(fixture); }
/// <summary> /// Gets the property of the specified type with the specified key. /// </summary> /// <typeparam name="TProperty">The type of the property.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="key">The key.</param> /// <returns></returns> public static TProperty GetProperty <TProperty>(this IMvcFunctionalTestFixture fixture, string key) { if (!fixture.Properties.ContainsKey(key)) { throw new ArgumentException("Cannot find test fixture property with key: " + key); } if (fixture.Properties[key] is TProperty p) { return(p); } throw new ArgumentException($"Test fixture property with key: {key} is not a {typeof(TProperty)}"); }
/// <summary> /// Creates a collection of auto fixture constructed instances of the specified model. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="models">The models.</param> /// <param name="configurator">The configurator.</param> /// <returns></returns> public static IMvcFunctionalTestFixture HavingModels <TModel>(this IMvcFunctionalTestFixture fixture, out ICollection <TModel> models, Action <TModel> configurator = null) { models = fixture.CreateMany <TModel>(); if (configurator != null) { foreach (var model in models) { configurator(model); } } return(fixture); }
public static IMvcFunctionalTestFixture HavingConfiguredApiClient(this IMvcFunctionalTestFixture fixture) => fixture.HavingPathBase("api");
/// <summary> /// Runs the specified fixture action if a property exists in the fixture that matches the specified value. /// </summary> /// <param name="fixture">The fixture.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <param name="action">The action.</param> /// <returns></returns> public static IMvcFunctionalTestFixture WhenHavingStringProperty(this IMvcFunctionalTestFixture fixture, string key, string value, Action <IMvcFunctionalTestFixture> action) => fixture.WhenHavingStringProperty(key, x => x == value, action);
/// <summary> /// Adds the property value generated by the specified factory to the specified key. /// </summary> /// <param name="fixture">The fixture.</param> /// <param name="key">The key.</param> /// <param name="valueFactory">The value factory.</param> /// <returns></returns> public static IMvcFunctionalTestFixture HavingProperty(this IMvcFunctionalTestFixture fixture, string key, Func <object> valueFactory) => fixture.HavingProperty(key, valueFactory());
/// <summary> /// Adds the specified property value to the specified key. /// </summary> /// <param name="fixture">The fixture.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <returns></returns> public static IMvcFunctionalTestFixture HavingProperty(this IMvcFunctionalTestFixture fixture, string key, object value) { fixture.Properties[key] = value; return(fixture); }
/// <summary> /// Configures the specified fixture's act step to be a POST request for the specified entity with the specified JSON body. /// </summary> /// <typeparam name="TBody">The type of the body.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="entity">The entity.</param> /// <param name="body">The body.</param> /// <returns></returns> public static IMvcFunctionalTestFixture WhenCreating <TBody>(this IMvcFunctionalTestFixture fixture, string entity, TBody body) => fixture.WhenCallingRestMethod(HttpMethod.Post, entity, body);
public static IMvcFunctionalTestFixture HavingDatabaseWithMultipleBreakfastItems(this IMvcFunctionalTestFixture fixture, out ICollection <BreakfastItem> items) { items = fixture.CreateMany <BreakfastItem>(); return(fixture.HavingDatabaseWithBreakfastItems(items.ToArray())); }
public static IMvcFunctionalTestFixture HavingDatabaseWithSingleBreakfastItem(this IMvcFunctionalTestFixture fixture, out BreakfastItem item) { item = fixture.Create <BreakfastItem>(); return(fixture.HavingDatabaseWithBreakfastItems(item)); }
public static IMvcFunctionalTestFixture HavingDatabaseWithBreakfastItems(this IMvcFunctionalTestFixture fixture, params BreakfastItem[] items) => fixture.HavingMySqlDatabase <BreakfastContext>(c => c.AddRange(items));
/// <summary> /// Adds an assertion to check that an entity of the specified type exists in the database, also running the specified assertions against it. /// </summary> /// <typeparam name="TDbContext">The type of the database context.</typeparam> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="id">The identifier.</param> /// <param name="assertions">The assertions.</param> /// <returns></returns> public static IMvcFunctionalTestFixture ShouldExistInDatabase <TDbContext, TEntity>(this IMvcFunctionalTestFixture fixture, object id, params Action <TEntity>[] assertions) where TDbContext : DbContext where TEntity : class => fixture.PostRequestResolvedServiceShould <TDbContext>(async ctx => { var existing = await ctx.Set <TEntity>().FindAsync(id); existing.Should().NotBeNull(); using (var aggregator = new ExceptionAggregator()) { foreach (var assertion in assertions) { aggregator.Try(() => assertion(existing)); } } });
/// <summary> /// Configures the specified fixture's act step to be a PATCH request for the specified entity and id with the specified JSON body. /// </summary> /// <typeparam name="TId">The type of the identifier.</typeparam> /// <typeparam name="TBody">The type of the body.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="entity">The entity.</param> /// <param name="id">The identifier.</param> /// <param name="body">The body.</param> /// <returns></returns> public static IMvcFunctionalTestFixture WhenPatching <TId, TBody>(this IMvcFunctionalTestFixture fixture, string entity, TId id, TBody body) => fixture.WhenCallingRestMethod(Patch, $"{entity}/{Uri.EscapeDataString(id.ToString())}", body);
/// <summary> /// Configures the specified fixture's act step to be a POST request for the specified entity with the specified JSON body. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="entity">The entity.</param> /// <param name="model">The model.</param> /// <returns></returns> public static IMvcFunctionalTestFixture WhenCreating <TModel>(this IMvcFunctionalTestFixture fixture, string entity, out TModel model) => fixture.WhenCallingRestMethod(HttpMethod.Post, entity, out model);
/// <summary> /// Adds an assertion to check that an entity of the specified type does not exist in the database. /// </summary> /// <typeparam name="TDbContext">The type of the database context.</typeparam> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <param name="fixture">The fixture.</param> /// <param name="id">The identifier.</param> /// <returns></returns> public static IMvcFunctionalTestFixture ShouldNotExistInDatabase <TDbContext, TEntity>(this IMvcFunctionalTestFixture fixture, object id) where TDbContext : DbContext where TEntity : class => fixture.PostRequestResolvedServiceShould <TDbContext>(async ctx => { var existing = await ctx.Set <TEntity>().FindAsync(id); existing.Should().BeNull(); });
public static IMvcFunctionalTestFixture HavingEmptyDatabase(this IMvcFunctionalTestFixture fixture) => fixture.HavingMySqlDatabase <BreakfastContext>();