Moq, SetupGet, Mocking a property

后端 未结 2 919
故里飘歌
故里飘歌 2020-12-13 23:15

I\'m trying to mock a class, called UserInputEntity, which contains a property called ColumnNames: (it does contain other properties, I\'ve just si

相关标签:
2条回答
  • 2020-12-13 23:37

    But while mocking read-only properties means properties with getter method only you should declare it as virtual otherwise System.NotSupportedException will be thrown because it is only supported in VB as moq internally override and create proxy when we mock anything.

    0 讨论(0)
  • 2020-12-13 23:44

    ColumnNames is a property of type List<String> so when you are setting up you need to pass a List<String> in the Returns call as an argument (or a func which return a List<String>)

    But with this line you are trying to return just a string

    input.SetupGet(x => x.ColumnNames).Returns(temp[0]);
    

    which is causing the exception.

    Change it to return whole list:

    input.SetupGet(x => x.ColumnNames).Returns(temp);
    
    0 讨论(0)
提交回复
热议问题