I'm new in writing tests and Mock. I'm trying to figure it out how to mock Raw SQL to retrieve data. Here's what I have:
I have DataContext.cs
public class DataContext : DbContext
{
public DataContext()
: base("Main")
{
}
public virtual DbSet<DbBook> Books { get; set; }
public virtual DbSet<DbMovie> Movies { get; set; }
}
I have controller BooksController.cs
public class BooksController : ApiController
{
private readonly BookDataContext _db;
public BooksController ()
{
_db = new BookDataContext();
}
public BooksController (BookDataContext context)
{
_db = context;
}
[HttpGet]
[Route("books")]
public Book GetBooks()
{
using (var dbContextTransaction = _db.Database.BeginTransaction())
{
var test = _db.Books.SqlQuery("select * from BOOK");
var test2 = from b in _db.Books
orderby b.Books
select b;
}
}
}
And I have test
[TestMethod]
public void GetBook()
{
var data = new List<DbBook>
{
new DbBook{ Book = "Book1"},
new DbBook{ Book = "Book2"}
}.AsQueryable();
var mockSet = new Mock<DbSet<DbScriptId>>();
mockSet.As<IQueryable<DbBook>>().Setup(m =>
m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<DbBook>>().Setup(m =>
m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<DbBook>>().Setup(m =>
m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<DbBook>>().Setup(m =>
m.GetEnumerator()).Returns(data.GetEnumerator());
mockSet.Setup(m => m.Find(It.IsAny<object[]>()))
.Returns<object[]>(sc => data.SingleOrDefault());
var mockContext = new Mock<DataContext>();
mockContext.Setup(c => c.Books).Returns(mockSet.Object);
var controller = new BooksController (mockContext.Object);
var books = controller.GetBook();
}
I can retrieve data using Linq (test2) but using SqlQuery I always get null (test). How to change that using SqlQuery I get same result as using Linq? Also I'm getting to know that creating InMemory database actually creates some lists (type=IQueriable) in background, not database. Can you please provide some explanation and resolution for this issue?
How about this. (not sure if I understand your question right)
var queryMock = new Mock<DbSqlQuery<DbBook>>();
queryMock.Setup(x => x.GetEnumerator()).Returns(data.GetEnumerator());
mockSet.Setup(m => m.SqlQuery(It.IsAny<string>(), It.IsAny<object[]>())).Returns(queryMock.Object);
This example assumes that all queries will return all items.
Related
So I've trying to test my webapp coded in .net core 5.0, and I have a classic MVC model with service classes. Now I am trying to unit test the solution with moq to mock my database, and my tests run, but I have just noticed that they are all wrong when I debug. It seems as if it doesn't actually connect to the mock service or database itself... I have gone for mocking the service interface but that doesn't seem to work still. Help would be greatly appreciated.
Service class:
public class InventoryService : IInventoryService
{
private readonly DBContext _db;
public InventoryService(DBContext db)
{
_db = db;
}
public List<Inventory> GetInventories(string id)
{
var inventories = (from i in _db.Inventories where i.userId.Equals(id) select i).ToList();
return inventories;
}
public void CreateInventory(Inventory newInventory)
{
_db.Inventories.Add(newInventory);
_db.SaveChanges();
}
public bool DeleteInventory(Guid Id, string UserId)
{
var inventory = _db.Inventories.Find(Id);
if (inventory == null)
return false;
if (inventory.userId != UserId)
return false;
//Delete items using item service
var items = from i in _db.Items where i.inventoryId.Equals(Id) select i;
foreach(var i in items)
{
_db.Items.Remove(i);
}
_db.Inventories.Remove(inventory);
_db.SaveChanges();
return true;
}
}
}
Service interface it uses:
public interface IInventoryService
{
public List<Inventory> GetInventories(string id);
public void CreateInventory(Inventory newInventory);
public bool DeleteInventory(Guid Id, string UserId);
}
}
Model:
public class Inventory
{
// The Id field is a unique identifier for a specific inventory
[Key]
public Guid Id { get; set; }
// The name of the Inventory. Required to have a value
[Required]
[DisplayName("Inventory Name")]
public string name { get; set; }
public string userId { get; set; }
}
}
Unit test:
public class InventoryTests
{
// Unit test defined for the get user inventories, a valid ID will always be passed in so no need for negative testing
[Fact]
public void Get_user_inventories_with_valid_id()
{
//ARRANGE
Guid theId1 = new("00000000-0000-0000-0000-000000000001");
Guid theId2 = new("00000000-0000-0000-0000-000000000002");
string u1 = "xxx";
string u2 = "yyy";
var mockIn = new Mock<IInventoryService>();
var data = new List<Inventory>
{
new Inventory { Id = theId1, name = "Mums 1", userId = u1},
new Inventory { Id = theId1, name = "Mums 1.2", userId = u1},
}.AsQueryable();
var mockSet = new Mock<DbSet<Inventory>>();
mockSet.As<IQueryable<Inventory>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Inventory>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Inventory>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Inventory>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
Mock<IInventoryService> myser = new Mock<IInventoryService>();
//ACT
myser.Setup(x => x.GetInventories(u1)).Returns(mockSet.Object.ToList());
var tinvs = myser.Object.GetInventories(u1);
//ASSERT
Assert.Equal(2 , tinvs.Count);
}
//Unit test for creating an inventory, a valid new inventory object will always be passed in so need for negative testing
[Fact]
public void Creating_An_Inventory()
{
//ARRANGE
Guid theId2 = new("00000000-0000-0000-0000-000000000001");
string u1 = "xxx";
string u2 = "yyy";
var mockIn = new Mock<IInventoryService>();
var data = new List<Inventory>
{
}.AsQueryable();
var mockSet = new Mock<DbSet<Inventory>>();
mockSet.As<IQueryable<Inventory>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Inventory>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Inventory>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Inventory>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
//ACT
Mock<IInventoryService> myser = new Mock<IInventoryService>();
myser.Setup(x => x.GetInventories(u1)).Returns(mockSet.Object.ToList());
Inventory thenew = new Inventory { Id = theId2, name = "Mums 2", userId = u2 };
myser.Object.CreateInventory(thenew);
//ASSERT
Assert.NotNull(data);
}
//Unit test for deleting an inventory. It will always be valid because a guid and user id will be passed in automatically
[Fact]
public void Deleting_An_Inventory()
{
//ARRANGE
Guid theId1 = new("00000000-0000-0000-0000-000000000001");
Guid theId2 = new("00000000-0000-0000-0000-000000000002");
string u1 = "123";
//var mockIn = new Mock<IInventoryService>();
var data = new List<Inventory>
{
new Inventory { Id = theId1, name = "testinv 1", userId = u1},
}.AsQueryable();
var mockSet = new Mock<DbSet<Inventory>>();
mockSet.As<IQueryable<Inventory>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Inventory>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Inventory>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Inventory>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
//ACT
Mock<IInventoryService> myser = new Mock<IInventoryService>();
myser.Setup(x => x.GetInventories(u1)).Returns(mockSet.Object.ToList());
myser.Object.DeleteInventory(theId1, u1);
var updInvs = myser.Object.GetInventories(u1);
int x = updInvs.Count;
//ASSERT
Assert.Equal(0, x);
}
}
}
I am fairly sure it is a minor error in my setup of the unit test's mocking. any help would be great.
So, if you want to test your InventoryService, you have to mock your dbContext as you do. Better use EF InMemoryDatabase like Microsoft advises https://learn.microsoft.com/en-us/ef/core/testing/#unit-testing
About your tests, I make a few modifications, to test InventoryService. It passes in debug mode.
public class InventoryService : IInventoryService
{
private readonly DBContext _db;
public InventoryService(DBContext db)
{
_db = db;
}
public List<Inventory> GetInventories(string id)
{
//i change inventories to _db.Set<Inventory> which able to be mock
var inventories = (from i in _db.Set<Inventory>() where i.userId.Equals(id) select i).ToList();
return inventories;
}
//ANOTHER CODE
}
[Fact]
public void Get_user_inventories_with_valid_id()
{
//ARRANGE
Guid theId1 = new("00000000-0000-0000-0000-000000000001");
Guid theId2 = new("00000000-0000-0000-0000-000000000002");
string u1 = "xxx";
string u2 = "yyy";
var mockIn = new Mock<IInventoryService>();
var data = new List<Inventory>
{
new Inventory { Id = theId1, name = "Mums 1", userId = u1 },
new Inventory { Id = theId1, name = "Mums 1.2", userId = u1 },
}.AsQueryable();
var mockSet = new Mock<DbSet<Inventory>>();
mockSet.As<IQueryable<Inventory>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Inventory>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Inventory>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Inventory>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
//HERE
var dbMock = new Mock<DBContext>();
dbMock.Setup(x => x.Set<Inventory>()).Returns(mockSet.Object);
var myser = new InventoryService(dbMock.Object);
//ACT
//AND HERE
var tinvs = myser.GetInventories(u1);
//ASSERT
Assert.Equal(2, tinvs.Count);
}
If you want to make InMemory db or test on real SQL db, you look at these questions.
Unit testing EF Core using in-memory database with an eager-loaded function
Unit testing with EF Core and in memory database
I have a service class like this:
public class CategoryService: ICategoryService
{
private myContext _context;
public CategoryService(myContext context)
{
_context = context;
}
public async Task<List<CategoryDTO>> GetCategories()
{
return (await _context.Categories.ToListAsync()).Select(c => new CategoryDTO
{
CategoryId = c.CategoryId,
CategoryName = c.CategoryName
}).ToList();
}
}
My context looks like this:
public DbSet<Category> Categories {get;set;}
My unit test for GetCategories() is:
[Fact]
public void TestGetCategories()
{
//Arrange
Mock <myContext> moq = new Mock <myContext>();
var moqSet = new Mock<DbSet<Category>>();
moq.Setup(m => m.Categories).Returns(moqSet.Object);
CategoryService service = new CategoryService(moq.Object);
//Act
var result = service.GetCategories();
//Assert
Assert.NotNull(result);
}
But I am getting error for my unit test. It says:
System.NotSupportedException : Unsupported expression: m => m.Categories
Can someone help me to fix the Setup part?
I finally could figure it out.
As #PeterCsala mentioned, we can use "EntityFrameworkCore3Mock"
You can find it here: https://github.com/huysentruitw/entity-framework-core3-mock
My unit test looks like this:
public DbContextOptions<ShoppingCartContext> dummyOptions { get; } = new DbContextOptionsBuilder<ShoppingCartContext>().Options;
[Fact]
public async Task TestGetCategories()
{
//Arrange
var dbContextMoq = new DbContextMock<ShoppingCartContext>(dummyOptions);
//Create list of Categories
dbContextMoq.CreateDbSetMock(x => x.Categories, new[]
{
new Category { CategoryId = 1, CategoryName = "Items" },
new Category { CategoryId = 2, CategoryName = "Fruits" }
});
//Act
CategoryService service = new CategoryService(dbContextMoq.Object);
var result = await service.GetCategories();
//Assert
Assert.NotNull(result);
}
You cannot use Moq with non overrideable properties. It needs to be either abstract or virtual and that's why you get the error.
Change the dbcontext property Categories to virtual and try again.
public virtual DbSet<Category> Categories {get;set;}
P.s. you don't need to do this when you mock interface methods, because they are inherently overridable.
I am failing on the BeUniqueEmail with the error message (below). How can I mock this correctly? I have included the validator and the test below.
The tests passed when I removed the BeUnique email validation
Message:
System.NotImplementedException : The method or operation is not implemented.
public class CreateStudentCommandValidator : AbstractValidator<CreateStudentCommand>
{
private readonly IApplicationDbContext _context;
public CreateStudentCommandValidator(IApplicationDbContext context)
{
_context = context;
RuleFor(v => v.Email)
.NotEmpty().WithMessage("Email is required.")
.MaximumLength(30).WithMessage("Email must not exceed 30 characters.")
.MustAsync(BeUniqueEmail).WithMessage("The specified email already exists.");
}
public async Task<bool> BeUniqueEmail(CreateStudentCommand model, string email, CancellationToken cancellationToken)
{
bool emailExists = await _context.Students
.Where(x => x.Email == email)
.Where(x => !x.IsDeleted)
.CountAsync() > 0;
return !emailExists;
}
}
Testing
[Test]
public async Task CreateStudentCommand_Success()
{
var mockSet = new Mock<DbSet<Student>>();
var context = new Mock<IApplicationDbContext>();
context.Setup(m => m.Student).Returns(mockSet.Object);
var handler = new CreateStudentCommandHandler(context.Object);
var validator = new CreateStudentCommandValidator(context.Object);
//var mockedValidator = new Mock<IValidator<CreateStudentCommandValidator>>();
//var mock1 = new Mock<AbstractValidator<CreateStudentCommandValidator>>();
var command = new CreateStudentCommand
{
StudentType = "Test1",
Email = "Test1#email.com",
FirstName = "Test1",
LastName = "Test1",
IsActive = true
};
var result = await handler.Handle(command, new CancellationToken());
// Act
var validationResult = await validator.ValidateAsync(command);
// Assert
Assert.True(validationResult.IsValid);
Assert.IsInstanceOf<Guid>(result);
}
Using Moq, a solution to this could be as follows.
[Test]
public async Task TestValidation()
{
var context = Mock.Of<ApplicationDbContext>();
var validator = new CreateStudentCommandValidator(context);
var command = new CreateStudentCommand
{
StudentType = "Test1",
Email = "Test1#email.com",
FirstName = "Test1",
LastName = "Test1",
IsActive = true
};
var validationResult = await validator.ValidateAsync(command);
Assert.True(validationResult.IsValid);
}
Not knowing your Context, I implemented a simple dummy.
public interface IApplicationDbContext
{
List<Student> Students { get; set; }
}
public class ApplicationDbContext : IApplicationDbContext
{
public ApplicationDbContext()
{
Students = new List<Student>();
}
public List<Student> Students { get; set; }
}
This all depends on the use of Moq however, If you are using some other Mocking Service the Implementation will change.
*My Previous answer was intended to explain the implementation of Mocking Method Results. But in this case you do not need to mock the CreateStudentCommandValidator
I'm trying to follow this guide for mocking Entity Framework
https://msdn.microsoft.com/en-us/library/dn314429.aspx
The code from the guide works absolutely fine when I build it in my project, but when I'm trying to apply it to my actual context and data objects, I'm getting an exception:
Object reference not set to an instance of an object
My object is pretty simple:
public class NodeAttributeTitle
{
public int ID { get; set; }
[MaxLength(150)]
public string Title { get; set; }
public string Description { get; set; }
}
as is my context
public class DataContext : DbContext
{
public virtual DbSet<NodeAttributeTitle> NodeAttributeTitles { get; set; }
}
and the method I'm trying to set is just a basic insertion
public class CommonNodeAttributes : ICommonNodeAttributes
{
private DataContext _context;
public CommonNodeAttributes(DataContext context)
{
_context = context;
}
public CommonNodeAttributes()
{
_context = new DataContext();
}
public void Insert(string value)
{
var nodeAttributeValue = new NodeAttributeValue();
nodeAttributeValue.Value = value;
nodeAttributeValue.Parent = 0;
_context.NodeAttributeValues.Add(nodeAttributeValue);
_context.SaveChanges();
}
}
And the test class is following the same syntax as in the MSDN guide
[TestClass]
public class CommonNodeAttributesTests
{
[TestMethod]
public void CreateNodeAttribute_saves_a_nodeattribute_via_context()
{
var mockSet = new Mock<DbSet<NodeAttributeTitle>>();
var mockContext = new Mock<DataContext>();
mockContext.Setup(m => m.NodeAttributeTitles).Returns(mockSet.Object);
var service = new CommonNodeAttributes(mockContext.Object);
service.Insert("blarg");
mockSet.Verify(m => m.Add(It.IsAny<NodeAttributeTitle>()),Times.Once());
mockContext.Verify(m => m.SaveChanges(),Times.Once);
}
}
and yet when the test runs, I get
Tests.CommonNodeAttributesTests.CreateNodeAttribute_saves_a_nodeattribute_via_context threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.
I don't understand why the code in the guide works fine, but my code doesn't.
I've tried adding 'virtual' to the ID, Title and Description properties but that doesn't do anything either. Does anyone have any clue what might be different for me?
You need to give some data in your mock:
IQueryable<NodeAttributeTitle> data = new List<NodeAttributeTitle>
{
new NodeAttributeTitle() {Id = 1, Title = "t1"},
new NodeAttributeTitle() {Id = 2, Title = "t2"},
}.AsQueryable();
var mockSet = new Mock<IDbSet<NodeAttributeTitle>>();
mockSet .Setup(m => m.Provider).Returns(data.Provider);
mockSet .Setup(m => m.Expression).Returns(data.Expression);
mockSet .Setup(m => m.ElementType).Returns(data.ElementType);
mockSet .Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
Then you can pass it to your DbContext:
Your problem in code is in the Add method ( _context.NodeAttributeValues.Add(nodeAttributeValue);) is not mocked!
I am trying to write a unit test but encountered a strange problem
[TestMethod]
public void Delete_user_save_via_context()
{
var data = new List<admins>
{
new admins() {id = 1, login = "test" },
}.AsQueryable();
var mockSet = new Mock<DbSet<admins>>(data);
mockSet.As<IQueryable<admins>>().Setup(x => x.Provider).Returns(data.Provider);
mockSet.As<IQueryable<admins>>().Setup(x => x.Expression).Returns(data.Expression);
mockSet.As<IQueryable<admins>>().Setup(x => x.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<admins>>().Setup(x => x.GetEnumerator()).Returns(data.GetEnumerator);
var mockContext = new Mock<Entities>();
mockContext.Setup(x => x.admins).Returns(mockSet.Object); //Here i have Exception
var userService = new UserService(mockContext.Object);
userService.Delete("test");
mockSet.Verify(m => m.Remove(It.IsAny<admins>()),Times.Once);
mockContext.Verify(m => m.SaveChanges(), Times.Once);
}
This throws exception :
Castle.DynamicProxy.InvalidProxyConstructorArgumentsException: Can not
instantiate proxy of class: Could not find a constructor that would
match given arguments: System.Linq.EnumerableQuery`
Can somebody help with this ?
Implementation of admin class :
public partial class admins
{
public int id { get; set; }
public string login { get; set; }
}
var mockSet = new Mock<DbSet<admins>>(data);
Should be:
var mockSet = new Mock<DbSet<admins>>();