Constructor Throws Exception When

2 posts, 0 answers
  1. Kevin
    Kevin avatar
    3 posts
    Member since:
    Mar 2012

    Posted 12 Oct 2012 Link to this post

    I have been reworking some classes to make them more testable.  I need to be able to test the constructor to make sure that it throws an exception when I have images that are different sizes.  The problem is that I am no longer creating this class using a List<Bitmap> because I need to know the file name for the image. 

    My question is how can I mock the property that is used to get the image list before creating an instance of the class to determine if it throws an exception?

    Update:

    At this point I have been able to get a valid test working using the following code and xUnit:

    public class MockTest
    {
        [Fact]
        public void ShouldThrowExceptionWhenImagesAreDifferentSizes()
        {                     
            Foo foo = null;
            Mock.Arrange(() => foo.GetImageList()).Returns(new List<Bitmap> { new Bitmap(100, 100), new Bitmap(200, 200) });
     
            Assert.Throws<ApplicationException>(() => foo = new Foo(null));
        }
     
        public class Foo
        {         
            public Foo(List<FileInfo> files)
            {
                this.CheckImageDimensions();          
            }
     
            public void CheckImageDimensions()
            {
                Bitmap firstImage = this.Images.FirstOrDefault();
                Bitmap invalidImage = this.Images.Where(x => x.Width != firstImage.Width || x.Height != firstImage.Height).FirstOrDefault();
     
                if (invalidImage != null)
                    throw new ApplicationException("Images with different dimensions are not supported.");
            }
      
            private List<Bitmap> _imageList;
                 
            public List<Bitmap> Images
            {
                get
                {
                    if (this._imageList != null)
                        return this._imageList;
                                                            
                    this._imageList = this.GetImageList();
                    return this._imageList;
                }
            }
                         
            public List<Bitmap> GetImageList()
            {
                throw new NotImplementedException();
            }
        }
    }

    The problem I have at the moment is that when I change:

    Mock.Arrange(() => foo.GetImageList()).Returns(new List<Bitmap> { new Bitmap(100, 100), new Bitmap(200, 200) });

    To

    Mock.Arrange(() => foo.Images).Returns(new List<Bitmap> { new Bitmap(100, 100), new Bitmap(200, 200) });

    The unit test will fail because it is not retuning the list of images that I created using Mock.Arrange().  Can someone please let me know what I need to do differently to get this to work?
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 15 Oct 2012 Link to this post

    Hi Kevin,
    Thanks again for reporting the issue.

    Basically, your following test should not work as well (a bug we fixed recently). This is because you are setting up and calling GetImageList for two different instances.

    [Fact]
    public void ShouldThrowExceptionWhenImagesAreDifferentSizes()
    {                    
        Foo foo = null;
        Mock.Arrange(() => foo.GetImageList()).Returns(new List<Bitmap> { new Bitmap(100, 100), new Bitmap(200, 200) });
     
        Assert.Throws<ApplicationException>(() => foo = new Foo(null));
    }

    This well falls under future mocking  and therefore foo.GetImageList and foo.Images should work as expected when IgnoreInstance is applied.

    Mock.Arrange(() => foo.Images).IgnoreInstance().Returns(new List<Bitmap> { new Bitmap(100, 100), new Bitmap(200, 200) });

    Hope this solves your issue.

    Kind Regards
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft R3 2016 release webinar banner
Back to Top