Here are the tests I am running for a school registration system : using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using SportsWeb.Models;
using Xunit;
namespace SportsWeb.Tests.Models
{
public class RegistrationViewModelValidationTests
{
private static IList Validate(object model)
{
var ctx = new ValidationContext(model, serviceProvider: null, items: null);
var results = new List();
Validator.TryValidateObject(model, ctx, results, validateAllProperties: true);
return results;
}
[Fact]
public void Missing_Season_IsInvalid()
{
// ARRANGE: Simulating a missing drop-down selection
var vm = new RegistrationViewModel
{
AthleteName = "Bram",
Season = null,
Sport = "Basketball"
};
// ACT
var results = Validate(vm);
// ASSERT
Assert.Contains(results, r => r.MemberNames.Contains(nameof(RegistrationViewModel.Season)));
}
[Fact]
public void Missing_Sport_IsInvalid()
{
// ARRANGE: Simulating no radio button being selected
var vm = new RegistrationViewModel
{
AthleteName = "Bram",
Season = "Fall",
Sport = null
};
var results = Validate(vm);
Assert.Contains(results, r => r.MemberNames.Contains(nameof(RegistrationViewModel.Sport)));
}
[Fact]
public void Valid_RegistrationModel_Passes()
{
// ARRANGE
var vm = new RegistrationViewModel
{
AthleteName = "Bram",
Season = "Winter",
Sport = "Basketball"
};
// ACT
var results = Validate(vm);
// ASSERT
Assert.Empty(results);
}
}
}
using System;
using SportsWeb.Models;
using SportsWeb.Services;
using Xunit;
namespace SportsWeb.Tests.Services
{
public class RegistrationServiceTests
{
private readonly IRegistrationService _service;
public RegistrationServiceTests()
{
// Testing the concrete service implementation
_service = new RegistrationService();
}
[Fact]
public void CalculateFee_ReturnsStandardRate_ForWinterBasketball()
{
// Arrange
var vm = new RegistrationViewModel { Season = "Winter", Sport = "Basketball" };
// Act
decimal fee = _service.CalculateFee(vm);
// Assert
Assert.Equal(150m, fee);
}
[Theory]
[InlineData("Summer", "Soccer", 100)]
[InlineData("Spring", "Tennis", 120)]
public void CalculateFee_ReturnsCorrectRates_ForAllSeasons(string season, string sport, decimal expectedFee)
{
// Arrange
var vm = new RegistrationViewModel { Season = season, Sport = sport };
// Act
decimal fee = _service.CalculateFee(vm);
// Assert
Assert.Equal(expectedFee, fee);
}
}
}
here are the controller tests : using Microsoft.AspNetCore.Mvc;
using Moq;
using SportsWeb.Controllers;
using SportsWeb.Models;
using SportsWeb.Services;
using Xunit;
namespace SportsWeb.Tests.Controllers
{
public class RegistrationControllerTests
{
private readonly Mock _mockService;
private readonly RegistrationController _controller;
public RegistrationControllerTests()
{
_mockService = new Mock<IRegistrationService>();
_controller = new RegistrationController(_mockService.Object);
}
[Fact]
public void CreatePost_InvalidModel_ReturnsSameView()
{
// ARRANGE
// Simulate ASP.NET Core catching a missing radio button selection
_controller.ModelState.AddModelError("Sport", "Please select a sport.");
var vm = new RegistrationViewModel { AthleteName = "Bram", Season = "Fall", Sport = null };
// ACT
var result = _controller.Create(vm) as ViewResult;
// ASSERT
Assert.NotNull(result);
Assert.False(_controller.ModelState.IsValid);
Assert.Same(vm, result.Model); // Ensure the form is sent back to the user with their inputs intact
// Verify the database/service was NOT called since the model was invalid
_mockService.VerifyNoOtherCalls();
}
[Fact]
public void CreatePost_ValidModel_CallsServiceAndRedirects()
{
// ARRANGE
var vm = new RegistrationViewModel { AthleteName = "Bram", Season = "Winter", Sport = "Basketball" };
// Set up the mock to return true when saving
_mockService.Setup(s => s.ProcessRegistration(vm)).Returns(true);
// ACT
var result = _controller.Create(vm) as RedirectToActionResult;
// ASSERT
Assert.NotNull(result);
Assert.Equal("Success", result.ActionName); // Ensure it redirects to the Success page
// Verify the service was called exactly once with the provided model
_mockService.Verify(s => s.ProcessRegistration(vm), Times.Once);
}
}
}
Top comments (0)