I am trying to create Unit test for below two methods using MsTest. I am fairly new to this, and so far have referred to different posts on the Topic.
Code Requirement
- Create a Timer Based Function (Azure)
- Execute Method 1 and Method 2 in the order to get the Output.
Test Requirement
- Ability to be able to create Unit test cases for each Class/Method with no external dependency (Fake/Mock)
- To Fit this Code under Test can be update as code is not yet Live.
- Open to other tools/Nugets beyond Moq to support the Test requirement.
When I try to run the Unit test, it does not mock Method 2 instead executes it. I need help in debugging the code.
public class Job: IJob
{
//Method 1
public List<TableEntity> GetJob()
{
var a = Get<T>("static value"); //Mock this to Test Method GetJob
return a.Result;
}
//Method 2
public async Task<List<T>> Get<T>(string tableName) where T : ITableEntity, new()
{
var t = new List<T>();
//add data to T
return t;
}
}
Interface
public interface IJob
{
List<TableEntity> GetJob();
Task<List<T>> Get<T>(string tableName) where T : ITableEntity, new();
}
Test Code
private readonly Mock<IJob> _mockIJob = new Mock<IJob>();
readonly Job _job = new Job();
public void NotThrow_Error_When_JobFound()
{
//Arrange
var jobs = new J.TableEntity()
{
FolderName = "FolderName",
Timestamp = DateTimeOffset.Now
};
var jobList = Task.FromResult(new List<TableEntity>() { jobs });
_mockIJob.Setup(c => c.Get<TableEntity>(""))
.Returns(jobList);
//Act
var actualResult = _job.GetJob();
//Assert
Assert.AreEqual(jobList, actualResult);
}