开发者

Mocking a call to an object with a property of type List

I am learning Moq, and I would like to mock an interface ISecureAsset that has a property Contexts which returns a list of SecurityContexts. I am testing a method on another class that accesses the Contexts property for a开发者_如何转开发uthorization.

public interface ISecureAsset {

   List<SecurityContext> Contexts { get; set; }
}

How can I do this with Moq? I want to be able to set the values in the Contexts list as well.


Just set up the property to return a fake list of SecurityContexts.

var mockAsset = new Mock<ISecureAsset>();

var listOfContexts = new List<SecurityContext>();
//add any fake contexts here

mockAsset.Setup(x => x.Contexts).Returns(listOfContexts);

The Moq quickstart guide may be of some assistance to you.


var mockSecureAsset = new Mock<ISecureAsset>();
mockSecureAsset.SetupGet(sa => sa.Contexts).Return(new List<SecurityContext>());

or

mockSecureAsset.SetupProperty(sa => sa.Contexts);
mockSecureAsset.Object.Contexts = new List<SecurityContext>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜