Skip to content

Commit 8830064

Browse files
committed
add SingleOrDefault test
1 parent 690ea21 commit 8830064

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace JsonApiDotNetCoreTests.Data.TestData
4+
{
5+
public class TestContext : DbContext
6+
{
7+
public TestContext(DbContextOptions options)
8+
: base(options)
9+
{
10+
}
11+
12+
public virtual DbSet<TodoItem> TodoItems { get; set; }
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace JsonApiDotNetCoreTests.Data.TestData
2+
{
3+
public class TodoItem
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; }
7+
}
8+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using JsonApiDotNetCoreTests.Data.TestData;
2+
using Microsoft.EntityFrameworkCore;
3+
using Moq;
4+
using Xunit;
5+
using JsonApiDotNetCore.Data;
6+
using System.Linq;
7+
using System;
8+
using System.Collections.Generic;
9+
10+
namespace JsonApiDotNetCoreTests.Data.UnitTests
11+
{
12+
public class GenericDataAccessTests
13+
{
14+
[Fact]
15+
public void SingleOrDefault_Fetches_SingleItemFromContext()
16+
{
17+
// arrange
18+
var data = new List<TodoItem>
19+
{
20+
new TodoItem { Id = 1, Name = "AAA" },
21+
new TodoItem { Id = 2, Name = "BBB" }
22+
}.AsQueryable();
23+
24+
//var mockSet = new Mock<IQueryable<TodoItem>>();
25+
var mockSet = new Mock<DbSet<TodoItem>>();
26+
mockSet.As<IQueryable<TodoItem>>().Setup(m => m.Provider).Returns(data.Provider);
27+
mockSet.As<IQueryable<TodoItem>>().Setup(m => m.Expression).Returns(data.Expression);
28+
mockSet.As<IQueryable<TodoItem>>().Setup(m => m.ElementType).Returns(data.ElementType);
29+
mockSet.As<IQueryable<TodoItem>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
30+
31+
32+
var genericDataAccess = new GenericDataAccess();
33+
34+
// act
35+
var item1 = genericDataAccess.SingleOrDefault<TodoItem>(mockSet.Object, "Id", 1);
36+
var item2 = genericDataAccess.SingleOrDefault<TodoItem>(mockSet.Object, "Id", 2);
37+
38+
// assert
39+
Assert.Equal(1, item1.Id);
40+
Assert.Equal(2, item2.Id);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)