示例#1
0
        public void Post_Run_adds_it_to_database()
        {
            Mock <DbSet <Run> > runSet  = new Mock <DbSet <Run> >();
            Mock <PGDbContext>  context = new Mock <PGDbContext>();

            context.Setup(m => m.Runs).Returns(runSet.Object);

            var runsController = new RunsController(context.Object);

            runsController.PostRun(GetTestRun());

            runSet.Verify(m => m.Add(It.IsAny <Run>()), Times.Once());
            context.Verify(m => m.SaveChanges(), Times.Once());
        }
示例#2
0
        public async Task PostRunOk()
        {
            IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddJsonFile("AppSettings.json");
            IConfiguration configuration = configurationBuilder.Build();

            RunService srv =
                new RunService(
                    new DBRunContext(new DbContextOptionsBuilder <DBRunContext>().UseInMemoryDatabase("DBRuns").Options),
                    configuration
                    );

            RunsController rc       = new RunsController(srv);
            var            identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);

            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "00000000-0000-0000-0000-000000000000"));
            identity.AddClaim(new Claim(ClaimTypes.Role, "USER"));

            rc.ControllerContext             = new ControllerContext();
            rc.ControllerContext.HttpContext = new DefaultHttpContext();
            rc.HttpContext.User = new ClaimsPrincipal(identity);



            // Arrange
            RunInput runInput01 =
                new RunInput()
            {
                Date     = new System.DateTime(2020, 6, 20, 19, 51, 0),
                Distance = 5600,
                Time     = 1100,
                Location = "Poggibonsi,IT"
            };


            // Act
            IActionResult retValue =
                await rc.PostRun(
                    new Guid("00000000-0000-0000-0000-000000000000"),
                    runInput01
                    );


            // Assert
            Assert.IsTrue(retValue is OkResult);
        }