C
C#•3mo ago
Rhys

Unit testing not working, doesn't hit multiple test classes

The first test class runs perfectly fine, but the second one doesnt even get hit. I cant debug or set any breakpoints as nothing is hit.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VehicleInspection_Domain.Interfaces;
using Moq;
using Microsoft.AspNetCore.Mvc;
using VehicleInspection_Domain.Models.AbstractModels;
using Newtonsoft.Json;

namespace VehicleInspection_Test
{
[TestClass]
public class VehicleInspectionTesting
{
private readonly IEmailRepository _emailRepository;

public VehicleInspectionTesting(IEmailRepository emailRepository)
{
_emailRepository = emailRepository;
}

[TestMethod]
public async Task Insepction_ReturnOkResult_AndSendsEmailOnMajorDefect()
{
string jsonData = @"
{
*** SOME DATA ***
}";

// Mock dependencies
var emailRepositoryMock = new Mock<IEmailRepository>();
var inspectionService = new VehicleInspectionTesting(emailRepositoryMock.Object);

// Act
var result = await inspectionService.ProcessInspection(jsonData);

// Assert
Assert.IsInstanceOfType(result, typeof(OkResult));

// Verify that SendCriticalDefectEmail was called when major defects exist
emailRepositoryMock.Verify(x => x.SendCriticalDefectEmail(It.IsAny<string>(), It.IsAny<int>()), Times.Once);
}
}
}
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VehicleInspection_Domain.Interfaces;
using Moq;
using Microsoft.AspNetCore.Mvc;
using VehicleInspection_Domain.Models.AbstractModels;
using Newtonsoft.Json;

namespace VehicleInspection_Test
{
[TestClass]
public class VehicleInspectionTesting
{
private readonly IEmailRepository _emailRepository;

public VehicleInspectionTesting(IEmailRepository emailRepository)
{
_emailRepository = emailRepository;
}

[TestMethod]
public async Task Insepction_ReturnOkResult_AndSendsEmailOnMajorDefect()
{
string jsonData = @"
{
*** SOME DATA ***
}";

// Mock dependencies
var emailRepositoryMock = new Mock<IEmailRepository>();
var inspectionService = new VehicleInspectionTesting(emailRepositoryMock.Object);

// Act
var result = await inspectionService.ProcessInspection(jsonData);

// Assert
Assert.IsInstanceOfType(result, typeof(OkResult));

// Verify that SendCriticalDefectEmail was called when major defects exist
emailRepositoryMock.Verify(x => x.SendCriticalDefectEmail(It.IsAny<string>(), It.IsAny<int>()), Times.Once);
}
}
}
https://gyazo.com/d591331eb72b053113bf56111c719a94
Gyazo
Gyazo
4 Replies
Pobiega
Pobiega•3mo ago
Is this MSTest? I would strongly urge you to use XUnit instead, if at all an option It had the most sane defaults and lifetime methods of all the test frameworks
Rhys
Rhys•3mo ago
@Pobiega How did that work.. I was using what I considered and official microsoft nuget package 😂 And I swapped it and it worked instantly wtf
Pobiega
Pobiega•3mo ago
😄 So, yeah, MSTest is an "official microsoft nuget" that doesnt make it good in fact, they use xunit and nunit for a lot of their own products