locked
MOQ mocking an interface with setter injection RRS feed

  • Question

  • Please the below code snippet

    public class FileUploadPresenter
    {
        private IFileRepository FileRepository
        {
            get { return UnityManager.Resolve<IFileRepository>(); }
        }
        public void LoadData(int id)
        {
             //How can I redirect the below ReadData call to Mocked method in the testcase ?bypassing the FileRepository getter ?
             List<FileModel> fileModelList = FileRepository.ReadData(Id);
            //Do something with the data
        }
    }

    My unit test case

    [TestMethod]
        public void TestMethod1()
        {
            var mock = new Mock<IFileRepository>();
            FileModelfm = new FileModel();
            fm.FileId = 1;
            fm.FileName = "abc";
            fm.PolicyTxnId = 10;
            List<FileModel> fml = new List<FileModel>();
            fml.Add(fm);
            mock.Setup(item => item.ReadData(10)).Returns(fml);        
            FileUploadPresenter FileUploadPresenterobj = new FileUploadPresenter();
            obj.LoadData();
        }

    Here my mocking code is not really working .How can I setup a mock such a way that the interface method call from my invoking class will hit the mocked method ? Here my interface property is private and readonly .





    • Edited by J05C Tuesday, August 5, 2014 1:03 PM
    • Moved by CoolDadTx Tuesday, August 5, 2014 2:15 PM Testing related
    Tuesday, August 5, 2014 11:58 AM

Answers

  • One of the fundamental things about mocking is that you need to be able to inject your mock instead of the real thing when you are testing.

    So you need a constructor or public property of your FileUploadPresenterthat accepts an IFileRepository

    So your code live will do:

    FileUploadPresenter FileUploadPresenterobj = new FileUploadPresenter(RealFileRepository);

    Whilst in the test

    FileUploadPresenter FileUploadPresenterobj = new FileUploadPresenter(mock);

    Or you can have a public property of type IFileRepository and set that once you new it up.

    Setter injection might imply the latter since it's the set of the public property is hit when you....ermmm.... set it.

    There might be some other issues in TestMethod1 but they pale by comparison.

    If you can't swop your mock in to an object or method you're testing at run time then you aren't mocking anything.


    Tuesday, August 5, 2014 12:22 PM
  • These forums are for MS products.  Please post questions related to third-party components in their forums (https://github.com/Moq/moq4).
    Tuesday, August 5, 2014 2:15 PM

All replies

  • One of the fundamental things about mocking is that you need to be able to inject your mock instead of the real thing when you are testing.

    So you need a constructor or public property of your FileUploadPresenterthat accepts an IFileRepository

    So your code live will do:

    FileUploadPresenter FileUploadPresenterobj = new FileUploadPresenter(RealFileRepository);

    Whilst in the test

    FileUploadPresenter FileUploadPresenterobj = new FileUploadPresenter(mock);

    Or you can have a public property of type IFileRepository and set that once you new it up.

    Setter injection might imply the latter since it's the set of the public property is hit when you....ermmm.... set it.

    There might be some other issues in TestMethod1 but they pale by comparison.

    If you can't swop your mock in to an object or method you're testing at run time then you aren't mocking anything.


    Tuesday, August 5, 2014 12:22 PM
  • These forums are for MS products.  Please post questions related to third-party components in their forums (https://github.com/Moq/moq4).
    Tuesday, August 5, 2014 2:15 PM