mocked collection .Add() fails

4 posts, 0 answers
  1. David
    David avatar
    1 posts
    Member since:
    Sep 2012

    Posted 13 Jun 2014 Link to this post

    Using VS 2012 update 4 w/ the latest version of JustMock (verified in the Telerik Control Panel).

    I have the following lines in my test:

                jobLogEntries = new List<job_log>();
                jobLogEntries.Add(new job_log
                {
                    job_log_ky = 27,
                });
                Mock.Arrange(() => this.Entities.job_log).IgnoreInstance().ReturnsCollection(jobLogEntries);

    I have the following lines inside the code being tested:
                    jobLog = new job_log
                    {
                        job_schedule_ky = this.JobScheduleRecord.job_schedule_ky,
                        job_name = this.JobScheduleRecord.job_name,
                        start_dt_tm = DateTime.Now,
                    };
                    ctx.job_log.Add(jobLog);

    During debugging I can see the job_log record that was added into the collection by the test, however the jobLog entity is not being added to the collection, causing the overall test to fail.

  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 18 Jun 2014 Link to this post

    Hello David,

    Thank you for contacting.

    As I can understand, the issue is that the ctx.job_log.Add(jobLog); line does not add the jobLog inside the jobLogEntries collection. Please, correct me if I have misunderstood you.

    Unfortunately, I did not manage to reproduce this on my side. For the purpose, I used the following test:
    [TestMethod]
    public void TestMethod()
    {
        // Arrange
        var testCollection = new List<Foo>();
        testCollection.Add(new Foo());
     
        var sut= new SUT();
        Mock.Arrange(() => sut.listProp).IgnoreInstance().ReturnsCollection(testCollection);
     
        // Act
        var realSUT = new SUT();
        realSUT.addToList();
     
        var actualReturnCount = realSUT.listProp.Count;
        var actualTestCollection = testCollection.Count;
     
        // Assert
        Assert.AreEqual(2, actualReturnCount);
        Assert.AreEqual(2, actualTestCollection);
    }

    and the system under test:
    public class SUT
    {
        public List<Foo> listProp {get; set;}
     
        public void addToList()
        {
            listProp.Add(new Foo());
        }
    }
     
    public class Foo
    {
         
    }

    To assist you further, I will need a sample project that reproduces the issue or some additional details that could guide me further. I am attaching the test solution to my reply, so that you can modify it in order to achieve the faulty behavior and return it back to me.

    I am looking forward to your reply.

    Regards,
    Kaloyan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. DevCraft R3 2016 release webinar banner
  4. James
    James avatar
    3 posts
    Member since:
    Jun 2014

    Posted 27 Jun 2014 Link to this post

    Hi Kaloyan,

    I'm seeing a similar issue in v2014.2.616.1. The code you supply above works fine. However, when I use the code below, adapted from the post here, the Add() does add the string to folders, but not to the list returned by testList.Folders.
    [TestMethod()]
    public void MockTest()
    {
        var testList = Mock.Create<SPList>();
        IList<SPListItem> folders = new List<SPListItem>();
     
        folders.Add(CreateEntity("Initial item"));
        Mock.Arrange(() => testList.Folders).ReturnsCollection(folders);
        Mock.Arrange(() => testList.Folders.Add(Arg.AnyString, SPFileSystemObjectType.Folder, Arg.AnyString))
            .Returns((string url, SPFileSystemObjectType type, string name) => AddEntity(folders, name));
         
        testList.Folders.Add("", SPFileSystemObjectType.Folder, "Test Folder");
        Assert.AreEqual(2, folders.Count, "The list 'folders' item count is incorrect");
        Assert.AreEqual(2, testList.Folders.Count, "The list 'testList.Folders' item count is incorrect");
    }
     
    private static SPListItem CreateEntity(string name)
    {
        var fakeItem = Mock.Create<SPListItem>();
     
        Mock.Arrange(() => fakeItem.Name).Returns(name);
     
        return fakeItem;
    }
     
    private static SPListItem AddEntity(IList<SPListItem> list, string name)
    {
        SPListItem item = CreateEntity(name);
     
        list.Add(item);
        return item;
    }

    Am I doing something screwy? Any help would be appreciated!

    Regards,
    Jim
  5. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 02 Jul 2014 Link to this post

    Hi James,

    Thank you for reporting this issue. It appears to be a bug in JustMock. We will fix this bug in one of our upcoming releases. As a token of gratitude for finding it, I've given you some Telerik points.

    As a temporary workaround I can suggest that you either do not use ReturnsCollection, or that you rearrange the Folders member after every modification to the collection, like so:

    Mock.Arrange(() => testList.Folders.Add(Arg.AnyString, SPFileSystemObjectType.Folder, Arg.AnyString))
        .Returns((string url, SPFileSystemObjectType type, string name) =>
        {
            AddEntity(folders, name);
            Mock.Arrange(() => testList.Folders).ReturnsCollection(folders);
        });

    I hope you find the workaround usable.

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top