using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace YourTestsNamespace
public class IncentiveServiceTests
private Mock<ICosmosSetupRepository> _noSqlRepositoryMock;
private Mock<IRepository> _sqlRepositoryMock;
private Mock<IEmployeeService> _employeeServiceMock;
private Mock<IFusionCache> _cacheStorageMock;
private Mock<ICharitySearchService> _searchServiceMock;
private Mock<ICharityService> _charityServiceMock;
private Mock<IDonationCheckService> _donationCheckServiceMock;
private Mock<IIncentiveApiService> _incentiveApiServiceMock;
private Mock<IDonationFeeService> _donationFeeServiceMock;
private Mock<IAffiliateFeatureService> _cosmosAffiliateFeatureServiceMock;
private Mock<IAuthenticatedMember> _authenticatedMemberMock;
private Mock<ICustomTextTranslationService> _customTextTranslationServiceMock;
private Mock<IFeaturePreviewService> _featurePreviewServiceMock;
private Mock<IIncentiveManagementApiService> _incentiveManagementApiServiceMock;
private Mock<IGetProjectsService> _getProjectsServiceMock;
private IncentiveService _incentiveService;
_noSqlRepositoryMock = new Mock<ICosmosSetupRepository>();
_sqlRepositoryMock = new Mock<IRepository>();
_employeeServiceMock = new Mock<IEmployeeService>();
_cacheStorageMock = new Mock<IFusionCache>();
_searchServiceMock = new Mock<ICharitySearchService>();
_charityServiceMock = new Mock<ICharityService>();
_donationCheckServiceMock = new Mock<IDonationCheckService>();
_incentiveApiServiceMock = new Mock<IIncentiveApiService>();
_donationFeeServiceMock = new Mock<IDonationFeeService>();
_cosmosAffiliateFeatureServiceMock = new Mock<IAffiliateFeatureService>();
_authenticatedMemberMock = new Mock<IAuthenticatedMember>();
_customTextTranslationServiceMock = new Mock<ICustomTextTranslationService>();
_featurePreviewServiceMock = new Mock<IFeaturePreviewService>();
_incentiveManagementApiServiceMock = new Mock<IIncentiveManagementApiService>();
_getProjectsServiceMock = new Mock<IGetProjectsService>();
_incentiveService = new IncentiveService(
_noSqlRepositoryMock.Object,
_searchServiceMock.Object,
_sqlRepositoryMock.Object,
_employeeServiceMock.Object,
_cacheStorageMock.Object,
_charityServiceMock.Object,
_donationCheckServiceMock.Object,
_incentiveApiServiceMock.Object,
_donationFeeServiceMock.Object,
_cosmosAffiliateFeatureServiceMock.Object,
_authenticatedMemberMock.Object,
_searchServiceMock.Object,
_customTextTranslationServiceMock.Object,
_featurePreviewServiceMock.Object,
_incentiveManagementApiServiceMock.Object,
_getProjectsServiceMock.Object
public async Task GetVolunteerEligibleIncentiveRules_ShouldReturnEligibleRules_WhenDataIsValid()
int affiliateEmployeeId = 123;
bool includeFutureParticipations = true;
string employeePayCurrencyCode = "USD";
bool includePrevious = true;
string masterGiveSingleCurrencyCode = "USD";
var mockResponse = new IncentiveEligibleRulesResponse
Rules = new List<IncentiveRuleEligibleResponse>
new IncentiveRuleEligibleResponse
EligibilityStartDate = DateTime.UtcNow.AddMonths(-1),
EligibilityEndDate = DateTime.UtcNow.AddMonths(1)
Programs = new List<IncentiveProgramEligibleResponse>
new IncentiveProgramEligibleResponse { ProgramId = "1" }
_incentiveApiServiceMock.Setup(x => x.GetVolunteerEligibleIncentiveRules(affiliateId, affiliateEmployeeId, includePrevious, masterGiveSingleCurrencyCode))
.ReturnsAsync(mockResponse);
var mockParticipationsFromRia = new List<IncentiveEligibleParticipation>
new IncentiveEligibleParticipation
ParticipationDate = DateTime.UtcNow,
EventType = EventTypes.Employee_IndividualActivity
var mockParticipationsFromEvent = new List<IncentiveEligibleParticipation>
new IncentiveEligibleParticipation
ParticipationDate = DateTime.UtcNow,
EventType = EventTypes.Employee
var mockParticipationsFromProject = new List<IncentiveEligibleParticipation>
new IncentiveEligibleParticipation
ParticipationDate = DateTime.UtcNow,
EventType = EventTypes.Project
_sqlRepositoryMock.Setup(x => x.All<VolunteerIndividualActivity>())
.Returns(mockParticipationsFromRia.AsQueryable());
_sqlRepositoryMock.Setup(x => x.All<Event>())
.Returns(mockParticipationsFromEvent.AsQueryable());
_getProjectsServiceMock.Setup(x => x.GetParticipationsFromProject(affiliateId, affiliateEmployeeId, null))
.ReturnsAsync(mockParticipationsFromProject);
var result = await _incentiveService.GetVolunteerEligibleIncentiveRules(
affiliateId, affiliateEmployeeId, includeFutureParticipations, employeePayCurrencyCode, includePrevious, masterGiveSingleCurrencyCode);
Assert.IsNotNull(result);
Assert.IsTrue(result.Count > 0);
_incentiveApiServiceMock.Verify(x => x.GetVolunteerEligibleIncentiveRules(affiliateId, affiliateEmployeeId, includePrevious, masterGiveSingleCurrencyCode), Times.Once);
_noSqlRepositoryMock.Object,
_searchServiceMock.Object,
_sqlRepositoryMock.Object,
_employeeServiceMock.Object,
_cacheStorageMock.Object,
_charityServiceMock.Object,
_donationCheckServiceMock.Object,
_incentiveApiServiceMock.Object,
_donationFeeServiceMock.Object,
_cosmosAffiliateFeatureServiceMock.Object,
_authenticatedMemberMock.Object,
_searchServiceMock.Object,
_customTextTranslationServiceMock.Object,
_featurePreviewServiceMock.Object,
_incentiveManagementApiServiceMock.Object,
_getProjectsServiceMock.Object
.Setup(x => x.GetParticipationsFromProject(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int?>()))
.ReturnsAsync(new List<IncentiveEligibleParticipation>
new IncentiveEligibleParticipation { Hours = 10, CharityId = 1, ParticipationDate = DateTime.UtcNow }
.Setup(x => x.GetParticipationsFromProject(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int?>()))
.ReturnsAsync(new List<IncentiveEligibleParticipation>
new IncentiveEligibleParticipation { Hours = 10, CharityId = 1, ParticipationDate = DateTime.UtcNow }
Test method YourCause.Services.Tests.Incentives.IncentiveEligibilityServiceTests.GetVolunteerEligibleIncentiveRules_ShouldReturnEligibleRules_WhenDataIsValid threw exception:
System.NotSupportedException: Unsupported expression: x => x.GetParticipationsFromProject(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int?>())
Non-overridable members (here: IncentiveService.GetParticipationsFromProject) may not be used in setup / verification expressions.
var volunteerActivities = new List<VolunteerIndividualActivity>
new VolunteerIndividualActivity { Id = 1, CharityId = 1, Name = "Activity1", Types = "Type1", Categories = "Category1", Active = true },
new VolunteerIndividualActivity { Id = 2, CharityId = 2, Name = "Activity2", Types = "Type2", Categories = "Category2", Active = true }
var participations = new List<Participation>
new Participation { Id = 1, AffiliateId = 1, AffiliateEmployeeId = 123, IndividualVolunteerActivityId = 1, Hours = 5, Active = true, ParticipationDate = DateTime.UtcNow.AddDays(-1) },
new Participation { Id = 2, AffiliateId = 1, AffiliateEmployeeId = 123, IndividualVolunteerActivityId = 2, Hours = 3, Active = true, ParticipationDate = DateTime.UtcNow.AddDays(-2) }
_sqlRepositoryMock.Setup(x => x.All<VolunteerIndividualActivity>()).Returns(volunteerActivities);
_sqlRepositoryMock.Setup(x => x.All<Participation>()).Returns(participations);
Test method YourCause.Services.Tests.Incentives.IncentiveEligibilityServiceTests.GetVolunteerEligibleIncentiveRules_ShouldReturnEligibleRules_WhenDataIsValid threw exception:
System.InvalidOperationException: The source IQueryable doesn't implement IDbAsyncEnumerable<YourCause.DTO.NewVolunteer.IncentiveEligibleParticipation>. Only sources that implement IDbAsyncEnumerable can be used for Entity Framework asynchronous operations. For more details see http://go.microsoft.com/fwlink/?LinkId=287068.
var participationsFromRia =
await (from via in _sqlRepository.All<VolunteerIndividualActivity>()
join p in _sqlRepository.All<Participation>() on via.Id equals p.IndividualVolunteerActivityId
p.AffiliateId == affiliateId && p.AffiliateEmployeeId == affiliateEmployeeId && p.Hours > 0 &&
p.IndividualVolunteerActivityId.HasValue && p.Active && p.Active
&& p.ParticipationDate >= minDate && p.ParticipationDate <= maxDate
select new IncentiveEligibleParticipation
CharityId = via.CharityId ?? 0,
ParticipationDate = p.ParticipationDate,
EventType = EventTypes.Employee_IndividualActivity,
Categories = via.Categories
The source IQueryable doesn't implement IDbAsyncEnumerable<YourCause.DTO.NewVolunteer.IncentiveEligibleParticipation>. Only sources that implement IDbAsyncEnumerable can be used for Entity Framework asynchronous operations. For more details see http://go.microsoft.com/fwlink/?LinkId=287068.