Ejemplo n.º 1
0
        public async Task Can_Get_TodoItems_Paginate_Check()
        {
            // Arrange
            await _context.ClearTableAsync <TodoItem>();

            await _context.SaveChangesAsync();

            var expectedResourcesPerPage = _fixture.GetRequiredService <IJsonApiOptions>().DefaultPageSize.Value;
            var person    = new Person();
            var todoItems = _todoItemFaker.Generate(expectedResourcesPerPage + 1);

            foreach (var todoItem in todoItems)
            {
                todoItem.Owner = person;
                _context.TodoItems.Add(todoItem);
                await _context.SaveChangesAsync();
            }

            var httpMethod = new HttpMethod("GET");
            var route      = "/api/v1/todoItems";
            var request    = new HttpRequestMessage(httpMethod, route);

            // Act
            var response = await _fixture.Client.SendAsync(request);

            var body = await response.Content.ReadAsStringAsync();

            var deserializedBody = _fixture.GetDeserializer().DeserializeMany <TodoItem>(body).Data;

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.NotEmpty(deserializedBody);
            Assert.True(deserializedBody.Count <= expectedResourcesPerPage, $"There are more items on the page than the default page size. {deserializedBody.Count} > {expectedResourcesPerPage}");
        }
        public InjectableResourceTests(TestFixture <TestStartup> fixture)
        {
            _fixture = fixture;
            _context = fixture.GetRequiredService <AppDbContext>();

            _personFaker = new Faker <Person>()
                           .RuleFor(t => t.FirstName, f => f.Name.FirstName())
                           .RuleFor(t => t.LastName, f => f.Name.LastName());
            _passportFaker = new Faker <Passport>()
                             .CustomInstantiator(f => new Passport(_context))
                             .RuleFor(t => t.SocialSecurityNumber, f => f.Random.Number(100, 10_000));
            _countryFaker = new Faker <Country>()
                            .RuleFor(c => c.Name, f => f.Address.Country());
        }
Ejemplo n.º 3
0
        public TodoItemControllerTests(TestFixture <TestStartup> fixture)
        {
            _fixture       = fixture;
            _context       = fixture.GetRequiredService <AppDbContext>();
            _todoItemFaker = new Faker <TodoItem>()
                             .RuleFor(t => t.Description, f => f.Lorem.Sentence())
                             .RuleFor(t => t.Ordinal, f => f.Random.Number())
                             .RuleFor(t => t.CreatedDate, f => f.Date.Past());

            _personFaker = new Faker <Person>()
                           .RuleFor(t => t.FirstName, f => f.Name.FirstName())
                           .RuleFor(t => t.LastName, f => f.Name.LastName())
                           .RuleFor(t => t.Age, f => f.Random.Int(1, 99));
        }
        public async Task Can_Fetch_Many_To_Many_Through_Id()
        {
            // Arrange
            var context    = _fixture.GetRequiredService <AppDbContext>();
            var article    = _articleFaker.Generate();
            var tag        = _tagFaker.Generate();
            var articleTag = new ArticleTag
            {
                Article = article,
                Tag     = tag
            };

            context.ArticleTags.Add(articleTag);
            await context.SaveChangesAsync();

            var route = $"/api/v1/articles/{article.Id}/tags";

            // @TODO - Use fixture
            var builder = WebHost.CreateDefaultBuilder()
                          .UseStartup <TestStartup>();
            var server = new TestServer(builder);
            var client = server.CreateClient();

            // Act
            var response = await client.GetAsync(route);

            // Assert
            var body = await response.Content.ReadAsStringAsync();

            Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with payload: {body}");

            var document = JsonConvert.DeserializeObject <Document>(body);

            Assert.Single(document.ManyData);

            var tagResponse = _fixture.GetDeserializer().DeserializeMany <Tag>(body).Data.First();

            Assert.NotNull(tagResponse);
            Assert.Equal(tag.Id, tagResponse.Id);
            Assert.Equal(tag.Name, tagResponse.Name);
        }
        public ManyToManyTests(TestFixture <TestStartup> fixture)
        {
            _fixture = fixture;
            var context = _fixture.GetRequiredService <AppDbContext>();

            _authorFaker = new Faker <Author>()
                           .RuleFor(a => a.LastName, f => f.Random.Words(2));

            _articleFaker = new Faker <Article>()
                            .RuleFor(a => a.Caption, f => f.Random.AlphaNumeric(10))
                            .RuleFor(a => a.Author, f => _authorFaker.Generate());

            _tagFaker = new Faker <Tag>()
                        .CustomInstantiator(f => new Tag())
                        .RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10));
        }
        public InjectableResourceTests(TestFixture <TestStartup> fixture)
        {
            _fixture = fixture;
            _context = fixture.GetRequiredService <AppDbContext>();

            _todoItemFaker = new Faker <TodoItem>()
                             .RuleFor(t => t.Description, f => f.Lorem.Sentence())
                             .RuleFor(t => t.Ordinal, f => f.Random.Number())
                             .RuleFor(t => t.CreatedDate, f => f.Date.Past());
            _personFaker = new Faker <Person>()
                           .RuleFor(t => t.FirstName, f => f.Name.FirstName())
                           .RuleFor(t => t.LastName, f => f.Name.LastName());
            _passportFaker = new Faker <Passport>()
                             .CustomInstantiator(f => new Passport(_context))
                             .RuleFor(t => t.SocialSecurityNumber, f => f.Random.Number(100, 10_000));
            _countryFaker = new Faker <Country>()
                            .RuleFor(c => c.Name, f => f.Address.Country());
            _visaFaker = new Faker <Visa>()
                         .RuleFor(v => v.ExpiresAt, f => f.Date.Future());
        }