dizel3d
dizel3d

Reputation: 3669

How to stub property getter?

I use Moq. I want to stub a property getter in mock object:

var db = new Lazy<DatabaseContext>(DatabaseContextFactory.Create);
var mock = new Mock<IDocumentOperationContext>();

// stub "Database" property getter
mock.SetupGet(_ => _.Database).Stub(_ => db.Value);

But it throws an exception:

System.InvalidOperationExceptionThe object 'IDocumentOperationContext _ => _.Database' is not a mocked object.

I don't want to implement IDocumentOperationContext or to stub with already existing DatabaseContext object.

How to achieve the goal? Is it possible in another Mocking library?

Upvotes: 1

Views: 134

Answers (1)

Rawling
Rawling

Reputation: 50184

Use .Returns instead of .Stub.

Upvotes: 1

Related Questions