tags:

views:

114

answers:

2

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 authorization.

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.

+1  A: 

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.

womp
Accepted in under a minute... is that some kind of record?
womp
A: 
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>();
Brian Genisio