0

I've read through the examples on JustMock (we have the paid version FYI) but am having difficulty in regards to updating a record (not adding).

I have a ILogRepo of

public interface ILogRepo
{
    DbSet<LogEntry> LogEntries { get; }
    LogContext LogContext { get; }
}

I have a LogInteraction of

public class LogInteraction : ILogInteraction
{
    private readonly ILogRepo _logRepo;

    public LogInteraction(ILogRepo logRepo)
    {
        _logRepo = logRepo;
    }

    public void UpdateLog(IList<AckDetail> ackDetails)
    {
        foreach (var ackDetail in ackDetails)
        {
            var logRecord = _logRepo.LogEntries.Single(r => r.CheckNum == ackDetail.CheckNo);
            logRecord.ProcessingStatus = ackDetail.Result.ToString();
            if (!string.IsNullOrWhiteSpace(ackDetail.Message))
            {
                logRecord.Message = ackDetail.Message;
            }
        }
        _logRepo.LogContext.SaveChanges();
    }
}

I've mocked out a fake LogEntries (it's an IList). And last but not least a test of

[TestFixture]
    public class LogTests
    {
        private ILogRepo _mockLogRepo;
        private ILogInteraction _uut;
        private IList<AckDetail> _fakeAckDetails;

        [SetUp]
        public void SetUp()
        {
            //Arrange
            _mockLogRepo = Mock.Create<ILogRepo>();

            _fakeAckDetails = FakeAckDetails();

            _uut = new LogInteraction(_mockLogRepo);
        }

        [Test]
        public void LogUpdated()
        {
            //Arrange
            var expectedResults = FakeLogEntries_AfterAckProcessing();
            var expectedJson = JsonConvert.SerializeObject(expectedResults);

            _mockLogRepo.Arrange(m => m.LogEntries).IgnoreInstance().ReturnsCollection(FakeLogEntries_AfterSending());

            //Act
            _uut.UpdateLog(_fakeAckDetails);
            var actualResults = _mockLogRepo.LogEntries.ToList();
            var actualJson = JsonConvert.SerializeObject(actualResults);

            //Assert
            Assert.AreEqual(expectedResults.Count, actualResults.Count);
            Assert.AreEqual(expectedJson, actualJson);
        }
    }

In my test, my _mockLogRepo is not being updated by my LogInteraction. Stepping through the code everything seems all well and good. If I inspect the context though and look for changes, that returns a false though. I think I've matched the example on Telerik's site pretty well, but I've only been able to find info on Adding (and by extrapolating, Removing). But since those two are actual methods and Updating isn't in Entity Framework I'm at a bit of a loss. My code will work in production, but I'd like it to work in Test too (kind of the whole point).

gilliduck
  • 2,762
  • 2
  • 16
  • 32
  • You configured `_mockLogRepo` to always return result of `FakeLogEntries_AfterSending`. What that function return actually? – Fabio May 03 '18 at 22:47
  • 1
    Not related, but seems like your repository leaking implementation details of entity framework. Instead you can have interface with methods: `GetRecordByCheckNo(int checkno)` and `UpdateRecord(Record record)`. – Fabio May 03 '18 at 22:54
  • @Fabio, normally it would return the contents of the DataBase, but I need it to return a mocked list of Log Entries (replicating what would be in the database prior to the Act portion of the test running. The SaveChanges() method just isn't updating that list (which I feel like it's kinda not supposed to, but if I'm supposed to then just re-fake the DB contents, what's the point? What am I testing at that point?). – gilliduck May 04 '18 at 03:04
  • Could you try using `.Returns()` and not `.ReturnsCollection()`? – Stefan Dragnev May 13 '18 at 21:01

0 Answers0