I have a method which uses IQueryable to get the value from another class, when I try to mock it normally and my expected result is always "System.Threading.Tasks.Task`1[Nop.Core.Domain.OnTrack.Road]" in Nunit. Can someone help me in resolving this issue?
Implementation Class:
public async Task<Track> OnTrack(int warId, int cardno, string trackName)
{
trackName = GetAssignedtrackName(trackName, cardno);
Track track;
using (var scope = TransactionScopeExtensions.AsyncScope())
{
track = await (from s in _trackRepository.Table
where s.Name == trackName && s.warId == warId
select s).FirstOrDefaultAsync();
if (track != null && track.CarVariantId.HasValue)
{
throw new ErrorCodeException(trackAssignmentErrorCode.AlreadyAssigned);
}
// Get car variant ID
var carVariantId =
(from cv in _carVariantRepository.Table
join c in _carRepository.Table on cv.CarId equals c.Id
where !c.Deleted && !cv.Deleted && c.Id == cardno
orderby cv.Published descending
select cv.Id).FirstOrDefault();
if (carVariantId == 0)
{
throw new ErrorCodeException(CatalogErrorCode.CarVariantNotFound);
}
if (track != null)
{
track.CarVariantId = carVariantId;
await _trackRepository.UpdateAsync(track);
}
else
{
track = new track
{
Name = trackName,
CarVariantId = carVariantId,
Quantity = 0,
warId = warId
};
await _trackRepository.InsertAsync(track);
}
scope.Complete();
}
return track;
}
Repository Code:
public partial interface IRepository<T> where T : BaseEntity
{
T GetById(object id);
Task<T> GetByIdAsync(object id);
void Insert(T entity, bool saveChanges = true);
Task InsertAsync(T entity, bool saveChanges = true);
void InsertRange(IEnumerable<T> entity, bool saveChanges = true);
Task InsertRangeAsync(IEnumerable<T> entity, bool saveChanges = true);
void Update(T entity, bool saveChanges = true);
Task UpdateAsync(T entity, bool saveChanges = true);
void Delete(T entity, bool saveChanges = true);
Task DeleteAsync(T entity, bool saveChanges = true);
int SaveChanges();
Task<int> SaveChangesAsync();
IQueryable<T> Table { get; }
void AttachAndUpdateProperty<TProperty>(T entity, params Expression<Func<T, TProperty>>[] modifiedProperties);
}
}
And my test Code looks like this:
IRepository<Track> _itrackRepository;
IRepository<Car> _icarRepository;
IRepository<CarVariant> _icarvariantRepository;
ITrackService _trackService;
Car _car1, _car2;
CarVariant _carvar1, _carvar2;
Track _track1, _track2;
[SetUp] // has all the mockup data
public void setup()
{
_track1 = new Track()
{
Id = 123,
Name = "PER123",
Quantity = 1,
CarVariantId = 123,
QuantityOnHold = 1,
WarId = 123,
};
_track2 = new Track()
{
Id = 11,
Name = "PER11",
Quantity = 1,
CarVariantId = 11,
};
_car1 = new Car()
{ Id = 123, Deleted = false, Name = "PER" };
_car2 = new Car()
{ Id = 11, Deleted = false, Name = "PER" };
_carvar1 = new CarVariant()
{
Name = "PER123",
Id = 123,
Deleted = false,
};
_carvar2 = new CarVariant()
{
Name = "PER11",
Id = 11,
Deleted = false
};
_itrackRepository=MockRepository.GenerateMock<IRepository<Track>>();
_itrackRepository.Expect(x => x.Table).Return(new List<Track>() { _track1, _track2 }.AsQueryable());
_icarRepository = MockRepository.GenerateMock<IRepository<Car>>();
_icarRepository.Expect(x => x.Table).Return(new List<Car>() { _car1, _car2 }.AsQueryable());
_icarvariantRepository = MockRepository.GenerateMock<IRepository<CarVariant>>();
_icarvariantRepository.Expect(x => x.Table).Return(new List<CarVariant>() { _carvar1, _carvar2 }.AsQueryable());
_trackService = new TrackService(_itrackRepository, _icarvariantRepository, _icarRepository);
}
[Test]
public async Task AssignTrack_test0001()
{
var temp = await (_trackService.OnTrack(321, 321, "fre"));
AreEqual("PER123", temp);
}
Aucun commentaire:
Enregistrer un commentaire