How to mock SetCommandTimeout and optionsBuilder using moq in .Net Core API

  • Thread starter Thread starter SomaSundaram R
  • Start date Start date
S

SomaSundaram R

Guest
I need a help to mock and handle the SetCommandTimeout and optionsBuilder for unit testing using Moq and NBuilder possibly?

Database Context:

public class ImportContext : DbContext, IImportContext

{
public ImportContext()
{
Database.SetCommandTimeout(150000);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(new WebServiceAppSettings().ParagonConnectionString);
}
public DbContext GetContext(Type dataContextType)
{
return (DbContext)Activator.CreateInstance(dataContextType);
}
public DbSet<Data> Data { get; set; }
public DbSet<SerialNum> SerialNum { get; set; }

}



Service:

public int GetSerialNo()
{
try
{
using (var context = new ImportContext())
{
var result = context.SerialNum.FirstOrDefault(p => p.SerialType == SerialNumType);
if (result != null)
{
var newHcpDataId = (Convert.ToInt32(result.SerialId) + 1);
result.SerialId = newDataId.ToString();
context.SaveChanges();
return newDataId;
}
return 0;
}
}
catch (Exception ex)
{
throw ex;
}
}




Test:

using System.Linq;
using FizzWare.NBuilder;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Services.Import.DBContext;
using Services.Import.Entities;
using Services.Import.Service;
using Services.Import.Test.Helper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;

namespace Services.Import.Test.Services
{
[TestClass]
public class ImportServiceTest
{
private Mock<IEmailService> _mockEmailService;
private Mock<IUtilityService> _mockUtilityService;
private Mock<IImportContext> _mockImportContext;
private ImportService _ImportService;
private readonly RandomGenerator _random = new RandomGenerator();
private readonly Mock<IImportContext> _mockContextFactory = new Mock<IImportContext>();

[TestInitialize]
public void TestSetup()
{
_mockImportContext = new Mock<IImportContext>();
_mockEmailService= new Mock<IEmailService>();
_mockUtilityService = new Mock<IUtilityService>();
_ImportService = new ImportService(_mockImportContext.Object, _mockUtilityService.Object, _mockEmailService.Object);
}

[TestMethod]
public void GetSerialNo_WhenItCalled_ShouldFindSerialNo()
{
// Arrange
var expectedResult = _random.Int();
var serialNum = Builder<SerialNum>.CreateListOfSize(5).All().Build();
_mockImportContext.Setup(mc => mc.SerialNum).Returns(ContextExtension.GetQueryableMockDbSet(serialNum.ToList()));

//Mock<Database> mockdatabase= new Mock<Database>("", _mockImportContext.Object);

// Act
var actualResult = _ImportService.GetSerialNo();

// Assert
Assert.AreEqual(expectedResult, actualResult);
}
}
}



When i execute the test for the case GetSerialNo_WhenItCalled_ShouldFindSerialNo it fails as it is missing the environment for SetCommandTimeout and optionsBuilder. When i comment those two my test is working fine.

Teaching me on how to mock the above test without any code changes in the DBContext will be really grateful.

Thank you for your support in advance.

Continue reading...
 

Similar threads

C
Replies
0
Views
85
Craig Muckleston (MCPD, MCTS)
C
B
Replies
0
Views
116
Born2Achieve
B
D
Replies
0
Views
117
Decompressor
D
Back
Top