Dear sir,
I am a big fan of telerik control. Recently I have convinced my boss to use use JustMock for my current project. Everything was running smooth and our company was totally impressed with it until I stuck in a problem. I have tried my level best to solve it but can't find a suitable solution. My colleague, Google, Stackoverflow everything failed to satisfy me, so finding no way I came to you with the hope that you would rescue me from this unsolvable problem. I have included my code here, please have a look on it.
Problem : Unable to Mock DbSet
we are test this Repository but don`t understand How to do it.
public class BaseRepository<T> : IRepositoryBase<T> where T : class, IEntity, new()
{
protected readonly DbContext DbContext;
protected DbSet<T> DbSet;
public BaseRepository(DbContext dbContext)
{
DbContext = dbContext;
DbSet = DbContext.Set<T>();
}
public void Add(T entity)
{
DbSet.Add(entity);
}
}
we are tried following code but we are failed.
public class BaseRepositoryTest
{
readonly TimeSketchContext _mockDataContext = Mock.Create<TimeSketchContext>();
private readonly BaseRepository<EmployeeSkill> _repository;
public BaseRepositoryTest()
{
Mock.Arrange(() => _mockDataContext.EmployeeSkill).ReturnsCollection(GetEmployeeSkills());
_repository = new BaseRepository<EmployeeSkill>(_mockDataContext);
}
[Fact]
public void Add()
{
var employeeSkill = GetEmployeeSkill();
_repository.Add(employeeSkill);
_mockDataContext.SaveChanges();
var allSkill = _repository.All();
Assert.Equal(2, allSkill.Count()); //we are get 0 item here and out test is failed.
}
[Fact]
public void Get_All_Test()
{
var allSkill = _repository.All();
Assert.Equal(1, allSkill.Count()); //we are get 0 item here and out test is failed.
}
#region Private Methods
private EmployeeSkill GetEmployeeSkill()
{
return new EmployeeSkill
{
SkillDescription = "aa",
SkillName = "bbbb",
Id = 1
};
}
private IEnumerable<EmployeeSkill> GetEmployeeSkills()
{
return new List<EmployeeSkill>
{
GetEmployeeSkill()
};
}
#endregion
}
if any books on it please refer.
I am a big fan of telerik control. Recently I have convinced my boss to use use JustMock for my current project. Everything was running smooth and our company was totally impressed with it until I stuck in a problem. I have tried my level best to solve it but can't find a suitable solution. My colleague, Google, Stackoverflow everything failed to satisfy me, so finding no way I came to you with the hope that you would rescue me from this unsolvable problem. I have included my code here, please have a look on it.
Problem : Unable to Mock DbSet
we are test this Repository but don`t understand How to do it.
public class BaseRepository<T> : IRepositoryBase<T> where T : class, IEntity, new()
{
protected readonly DbContext DbContext;
protected DbSet<T> DbSet;
public BaseRepository(DbContext dbContext)
{
DbContext = dbContext;
DbSet = DbContext.Set<T>();
}
public void Add(T entity)
{
DbSet.Add(entity);
}
}
we are tried following code but we are failed.
public class BaseRepositoryTest
{
readonly TimeSketchContext _mockDataContext = Mock.Create<TimeSketchContext>();
private readonly BaseRepository<EmployeeSkill> _repository;
public BaseRepositoryTest()
{
Mock.Arrange(() => _mockDataContext.EmployeeSkill).ReturnsCollection(GetEmployeeSkills());
_repository = new BaseRepository<EmployeeSkill>(_mockDataContext);
}
[Fact]
public void Add()
{
var employeeSkill = GetEmployeeSkill();
_repository.Add(employeeSkill);
_mockDataContext.SaveChanges();
var allSkill = _repository.All();
Assert.Equal(2, allSkill.Count()); //we are get 0 item here and out test is failed.
}
[Fact]
public void Get_All_Test()
{
var allSkill = _repository.All();
Assert.Equal(1, allSkill.Count()); //we are get 0 item here and out test is failed.
}
#region Private Methods
private EmployeeSkill GetEmployeeSkill()
{
return new EmployeeSkill
{
SkillDescription = "aa",
SkillName = "bbbb",
Id = 1
};
}
private IEnumerable<EmployeeSkill> GetEmployeeSkills()
{
return new List<EmployeeSkill>
{
GetEmployeeSkill()
};
}
#endregion
}
if any books on it please refer.