I wrote a unit test thats failing:
[Test]
public async Task GetLogsPaged_Account_ExpectsSuccess()
{
//Arrange
var request = new Mock<HttpRequest>();
request.Setup(x => x.Scheme).Returns("https");
request.Setup(x => x.Host).Returns(HostString.FromUriComponent("https://localhost:5001"));
request.Setup(x => x.PathBase).Returns(PathString.FromUriComponent("/v1/customersupport-manager/logs/account/D4C88E3C-2848-400F-AB66-0DC3FAFFD24A?PageNumber=1&PageSize=5&DateFrom=2021-09-30%2012%3A30%3A13.413&DateTo=2021-09-30%2012%3A33%3A05.317"));
List<LogEntry> logEntries = new List<LogEntry>() { new LogEntry { Id = 1 }, new LogEntry { Id = 2 } };
PaginationFilter paginationFilter = new PaginationFilter { };
LogEntryListFilter logEntryListFilter = new LogEntryListFilter { };
var accountId = Guid.NewGuid();
LogEntryPageResult logs = new LogEntryPageResult() { PagedRecords = logEntries };
A.CallTo(() => logRepo.GetLogs(A<Guid>._, A<PaginationFilter>._, A<LogEntryListFilter>._)).Returns(logs);
//Act
var result = await svc.GetLogs(accountId, request.Object, paginationFilter, logEntryListFilter);
//Assert
result.ApplicationEvents[0].Code.Should().Be(ApplicationResponseCodes.System.OperationSucceed);
}
When I debug the test I get a exception that says - {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}.
Which can only be var request because it's mocked. I'm not sure why I'm getting this exception when I've seen HttpRequest being mocked in other tests. Any assistance is appreciated.
Edit: I'm including the the line that fails in my code
var uri = new Uri(request.GetDisplayUrl()); <- System.NullReference
But I have the url specified in my tests :
request.Setup(x => x.Scheme).Returns("https");
request.Setup(x => x.Host).Returns(HostString.FromUriComponent("https://localhost:5001"));
request.Setup(x => x.PathBase).Returns(PathString.FromUriComponent("/v1/customersupport-manager/logs/account/D4C88E3C-2848-400F-AB66-0DC3FAFFD24A?PageNumber=1&PageSize=5"));
Related
I have the test below and am using standard mocking on a DbSet/Context. When the test runs it fails as it states that "The source IQueryable doesn't implement IAsyncEnumerable Team. Only sources that implement IAsyncEnumerable can be used for Entity Framework asynchronous operations".
public async Task Get_team_name_with_valid_search_term_returns_team_names()
{
// Arrange
var data = new List<Team>
{
new Team {Name = "Leeds"},
new Team {Name = "Glasgow"}
}.AsQueryable();
var mockSet = new Mock<DbSet<Team>>();
mockSet.As<IQueryable<Team>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Team>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Team>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Team>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
var mockContext = new Mock<RadContext>();
mockContext.Setup(c => c.Team).Returns(mockSet.Object);
var service = new ITeamSearchService(mockContext.Object);
// Act
var result = await service.GetTeamName("Gla");
// Assert
}
The service itself is quite simple
public async Task<List<SearchTeamResponse>> GetTeamName(string searchTerm)
{
if (searchTerm == null)
{
throw new ArgumentNullException(nameof(searchTerm));
}
var query = await _radContext.Team.Where(x => x.Name.StartsWith(searchTerm))
.OrderBy(x => x.Name)
.ToListAsync();
var json = JsonConvert.SerializeObject(query);
var result = JsonConvert.DeserializeObject<List<SearchTeamResponse>>(json);
return result;
}
I am writing an unit test to test my EF project . while doing the test I am getting an error while configuraing the fake for the DBSet.
My Call stack is
System.NotImplementedException
The member 'IQueryable.ElementType' has not been implemented on type
'DbSet`1Proxy_10' which inherits from 'DbSet`1'. Test doubles for 'DbSet`1'
must provide implementations of methods and properties that are used.
at
system.Data.Entity.Infrastructure.DbQuery`1.GetInternalQueryWithCheck(String
memberName)
at
My Unit Test Code is - I had added a mapping data
var socMappingList = new List<DFC_SocMappings>()
{
new DFC_SocMappings()
{
JobProfile = "Police Officer",
ONetCode = "111-00.01",
QualityRating = 4,
SocCode = "1120"
},
new DFC_SocMappings()
{
JobProfile = "Police Officer",
ONetCode = "111-00.02",
QualityRating = 4,
SocCode = "1121"
},
};
//Arrange
var applicationLogger = A.Fake<IApplicationLogger>();
var fakeIQueryable = new List<DFC_SocMappings>().AsQueryable();
var context = A.Fake<IObjectContextFactory<OnetRepositoryDbContext>>();
// var fakeDbSet = A.Fake<DbSet<DFC_SocMappings>>();
IMapper iMapper = new AutoMapper.Mapper(new MapperConfiguration(cfg => cfg.AddProfile(new SkillsFrameworkMapper())));
var fakeDbSet = A.Fake<DbSet<DFC_SocMappings>>((d => d.Implements(typeof(IQueryable<DFC_SocMappings>)).Implements(typeof(IDbAsyncEnumerable<DFC_SocMappings>)))).SetupData(socMappingList);
A.CallTo(() => context.GetContext().DFC_SocMappings).Returns(fakeDbSet);
var ct = A.Fake<OnetRepository>(op => op.WithArgumentsForConstructor(new object[] { context, iMapper, applicationLogger }));
//Act
A.CallTo(() => ((IQueryable<DFC_SocMappings>)fakeDbSet).Provider).Returns(fakeIQueryable.Provider);
A.CallTo(() => ((IQueryable<DFC_SocMappings>)fakeDbSet).Expression).Returns(fakeIQueryable.Expression);
A.CallTo(() => ((IQueryable<DFC_SocMappings>)fakeDbSet).ElementType).Returns(fakeIQueryable.ElementType);
A.CallTo(() => ((IQueryable<DFC_SocMappings>)fakeDbSet).GetEnumerator()).Returns(fakeIQueryable.GetEnumerator());
//Assert
var onetRespository = new OnetRepository(context, iMapper, applicationLogger);
var response = await onetRespository.GetAllSocMappingsAsync<DfcOnetSocMappings>();
var res = response; // failing while reading the data
A.CallTo(() => context.GetContext().Set<DFC_SocMappings>()).MustHaveHappened();
response.Should().NotBeNull();
response.Should().BeSameAs(socMappings);
onetRespository.Dispose();
Can someone tell what mistake I am doing.
I'm running into an issue where Moq doesn't return what I excpect using following code:
[TestMethod]
public void GetResultReturnsAResult()
{
var mockUnitOfWork = Arrange();
// Arrange
var controller = new ResultsController(mockUnitOfWork.Object);
controller.Request = new HttpRequestMessage();
controller.Configuration = new HttpConfiguration();
// Act
var response = controller.GetResult(2);
// Assert
Assert.IsTrue(response is OkNegotiatedContentResult<Result>);
var contentResult = response as OkNegotiatedContentResult<Result>;
Assert.IsNotNull(contentResult);
Assert.IsNotNull(contentResult.Content);
Assert.IsTrue(contentResult.Content.ID == 2);
}
[TestMethod]
public void GetResultReturnsNotFound()
{
var mockUnitOfWork = Arrange();
// Arrange
var controller = new ResultsController(mockUnitOfWork.Object);
controller.Request = new HttpRequestMessage();
controller.Configuration = new HttpConfiguration();
// Act
var response = controller.GetResult(100);
// Assert
Assert.IsTrue(response is NotFoundResult);
var contentResult = response as NotFoundResult;
Assert.IsNotNull(contentResult);
}
private Mock<IUnitOfWork> Arrange()
{
var results = new List<Result>()
{
new Result()
{
ID = 1,
Name = "Result 1",
Modified = new DateTime(2017, 1, 1),
Created = new DateTime(2017, 1, 1),
CreatedBy = "Tester 1",
ModifiedBy = "Tester 2"
},
new Result()
{
ID = 2,
Name = "Result 2",
Modified = new DateTime(2017, 1, 2),
Created = new DateTime(2017, 1, 2),
CreatedBy = "Tester 1",
ModifiedBy = "Tester 2"
},
};
var mockUnitOfWork = new Mock<IUnitOfWork>();
var mockResultRepository = new Mock<IGenericRepository<Result>>();
mockResultRepository.Setup(x => x.Get(null, null, ""))
.Returns(results);
mockResultRepository.Setup(x => x.GetById(It.IsAny<int>()))
.Returns((int id) => GetById(results, id));
mockUnitOfWork.Setup(x => x.ResultRepository)
.Returns(mockResultRepository.Object);
return mockUnitOfWork;
}
private Result GetById(List<Result> results, int id)
{
return results.FirstOrDefault(r => r.ID == id);
}
In this case my TestMethods testing GetById both throw an exception with message: Object of type 'System.Object[]' cannot be converted to type 'System.Int32'.
When I change the mocking code to this:
mockResultRepository.Setup(x => x.Get(null, null, ""))
.Returns(results);
mockUnitOfWork.Setup(x => x.ResultRepository)
.Returns(mockResultRepository.Object);
mockUnitOfWork.Setup(x => x.ResultRepository.GetById(It.IsAny<int>()))
.Returns((int id) => GetById(results, id));
The GetById tests don't throw an exception but GetResultReturnsAResult doesn't return a result so it always fails.
Can anyone shed a light on this behaviour?
As suggested by Eugene Podskal in the comments, I was creating an MCVE (download here) and ran into the solution. He was in fact right that the solution was to be found in code that was not shared first.
The code that returned the
Object of type 'System.Object[]' cannot be converted to type 'System.Int32'
exception was correct in throwing this exception since
GetById
takes a
params object[]
as first parameter.
I need to make changes to a legacy class with no tests so I have started by writing a test, however the mocking (using Moq) isn't working properly.
This my test
[Test]
public void CorrectlyCopiesToLightningWhenNoLocationsExist()
{
// arrange
long orgId = Id64.NewId();
var data = LightningMapLocationsHelperTestData.ForNormalCopy(orgId);
var organisation = new Group
{
GroupId = orgId,
Name = "Test Organisation",
Type = GroupType.OrganisationGroup
};
var groupRepo = new Mock<IGroupRepository>();
groupRepo.Setup(r => r.GetGroup(orgId)).Returns(organisation);
var orgRepo = Mock.Of<IOrganisationRepository>(o => o.LightningLocationsEnabledFor(orgId));
var mapLocationRepo = new Mock<IMapLocationRepository>();
mapLocationRepo.Setup(r => r.OrganisationRepository).Returns(orgRepo);
mapLocationRepo
.Setup(r => r.GetMapLocationsByGroupIds(orgId, It.IsAny<IEnumerable<long>>(), true, true))
.Returns(data.InitialDatabaseLocations);
var lightningMapLocationRepo = new Mock<ILightningMapLocationRepository>();
lightningMapLocationRepo
.Setup(r => r.LocationsById(orgId, data.InitialLightningLocations.Select(l => l.LocationId)))
.Returns(data.InitialLightningLocations);
lightningMapLocationRepo
.Setup(r => r.AddLocations(It.IsAny<List<Location>>()))
.Callback((List<Location> locations) => data.InitialLightningLocations.AddRange(locations));
var infoMessages = new List<string>();
var errorMessages = new List<string>();
var helper = new LightningMapLocationsHelper(
(string s, object[] args) => infoMessages.Add(string.Format(s, args)),
(string s, object[] args) => errorMessages.Add(string.Format(s, args)));
List<CopyFailure> copyFailures;
// act
bool success = helper.CopyLocationsForOrganisation(orgId, 10, out copyFailures);
// assert
success.ShouldBeTrue();
(errorMessages?.Count ?? 0).ShouldBe(0);
data.InitialLightningLocations.Count.ShouldBe(data.ExpectedLightningLocations.Count);
}
Inside LightningMapLocationsHelper is the following method
private Group GetValidOrganisationGroup(long groupId)
{
var organisation = (new GroupRepository()).GetGroup(groupId);
if (organisation != null && organisation.Type == GroupType.OrganisationGroup) return organisation;
LogErrorMessage("Invalid groupId: {0}. Ignoring...", groupId);
return null;
}
that when called is using an actual instance of GroupRepository rather than the groupRepo mock set up in the test, thus causing the test to fail. As GroupRepository implements IGroupRepository I expected this to work.
public class GroupRepository : IGroupRepository {…}
Perhaps I am misunderstanding how mocking works. Can someone offer some insight to help me understand why this doesn't work, and how I can fix it? Do I have to pass the mocked classes in?
I am mocking a function in c# using Moq.
public void Mapper_GetProductMappingTest()
{
//Arrange:
Mapping mapping= new Mapping()
{
code = "AB"
};
Mapper mapper = new Mapper(new MongoStore());
CaseProduct caseProduct = new CaseProduct()
{
ProductID = "ABC",
};
Mock<Mapper> mock = new Mock<Mapper>(new MongoStore());
mock.Setup(x => x.GetMapping(caseProduct)).Returns(mapping);
// why does it return the caseproduct not the mapping?!
var mapped = mock.Object.GetMapping(caseProduct);
Assert.IsTrue(mapped.code == "AB");
This Assert.IsTrue is failed and the mock.Object.GetMapping(caseProduct) returns ABC insteadof AB.
Can somebody help me with that?