c# 4.0 - How to MOQ a repository and ensure that the method is called using VerifyAll()? -
this repository class
using system; using system.collections.generic; using dealer.rails.common.utils; using dealer.rails.repository.entities.c3; using dealer.rails.repository.repositories.c3.interfaces; using dealer.rails.repository.repositories.soar; using microsoft.extensions.logging; using newtonsoft.json; namespace dealer.rails.repository.repositories.c3 { public class vehiclestagingrepository : baserepository, ivehiclestagingrepository { public vehiclestagingrepository(ilogger<vehiclestagingrepository> logger) : base(logger) { } public void savevehiclestaging(list<importstagevh> vehiclestagingrecords, c3context c3context) { using (var transaction = c3context.database.begintransaction()) { var vehiclestagingrecordbeforesaving = new importstagevh(); try { foreach (var vehiclestagingrecord in vehiclestagingrecords) { vehiclestagingrecordbeforesaving = vehiclestagingrecord; c3context.importstagevh.add(vehiclestagingrecord); c3context.savechanges(); } } catch (timeoutexception tex) { transaction.rollback(); _logger.logerror(loggingevents.saveemployeestaging, tex.message); throw; } catch (exception ex) { transaction.rollback(); _logger.logerror(loggingevents.saveemployeestaging, ex, jsonconvert.serializeobject(vehiclestagingrecordbeforesaving)); throw new exception(vehiclestagingrecordbeforesaving.tostring(),ex); } transaction.commit(); } } } }
this xunit test cases using moq.
using dealer.rails.repository.entities.c3; using dealer.rails.repository.repositories.c3; using dealer.rails.repository.repositories.c3.interfaces; using microsoft.entityframeworkcore; using microsoft.extensions.logging; using moq; using system.collections.generic; using xunit; namespace dealer.rails.repository.tests.repositories.rails { public class vehiclestagingrepositorytests { [fact] [trait("repositories", "c3")] public void should_dump_records_to_vehicle_staging_dump() { // arrange var optionsbuilder = new dbcontextoptionsbuilder<c3context>(); optionsbuilder.usesqlserver("data source=dev.c3.dealertrack.corp;initial catalog=c3_bmc;integrated security=true;multipleactiveresultsets=true;application name=entityframework"); var mocklogger = new mock<ilogger<vehiclestagingrepository>>(); var mockc3context = new c3context(optionsbuilder.options); // var vehiclestagingrecords = new list<importstagevh>(); var mockrepository = new mock<ivehiclestagingrepository>(); mockrepository.setup(x => x.savevehiclestaging(it.isany<list<importstagevh>>(), mockc3context)); // system under test templatecolumnrepository var sut = new vehiclestagingrepository(mocklogger.object); // act sut.savevehiclestaging(new list<importstagevh>(), mockc3context); // assert mockrepository.verify(); // mockrepository.verify(m=>m.savevehiclestaging(vehiclestagingrecords,mockc3context),times.once,"failed call savevehiclestaging method"); } } }
moq should allow me independently test repository. issues facing
in line optionsbuilder.usesqlserver.. if use optionsbuilder.usesqlserver("some dummy connection string"); code throws exception. opinion should not.
in line mockrepository.verify();, if use mockrepository.verifyall(), fails @ line
3. next commented line throwing error. in opinions should have executed savestagingvehicle method atleast once.
please guide me in resolving these issues.
Comments
Post a Comment