public void VerifyLazyMembersAreLazy() { // Mock a inherited class instance of AuthZyinContext<TestCustomData> var mockContext = new Mock <AuthZyinContext <TestCustomData> >(TestContext.DefaultPolicies, TestContext.DefaultClaimsPrincipal) { CallBase = true, }; var data = new TestCustomData(); mockContext.Protected().Setup <TestCustomData>("CreateData").Returns(data); var context = mockContext.Object; // Accessing data multiple times only triggers the CreateData method once var data1 = context.Data; mockContext.Protected().Verify("CreateData", Times.Once()); var data2 = context.Data; mockContext.Protected().Verify("CreateData", Times.Once()); Assert.Same(data1, data2); var data3 = context.GetData(); mockContext.Protected().Verify("CreateData", Times.Once()); Assert.Same(data1, data3); // Accessing GetDataAsJObject doesn't cause multiple JObject constructs var jObj1 = context.GetDataAsJObject(); Assert.NotNull(jObj1); mockContext.Protected().Verify("CreateData", Times.Once()); var jObj2 = context.GetDataAsJObject(); Assert.Same(jObj1, jObj2); }
public void EvaluateMethodThrowsWithNullCustomData() { var dataJPathIntValue = new TestCustomData().JPathIntValue; var context = new TestContext(setNullCustomData: true); var resource = new TestResource(); var equalsRequirement = new JsonPathRequirement <TestCustomData, TestResource>( OperatorType.Equals, dataJPathIntValue, resource.JPathIntValue, Direction.ContextToResource); // Throws if no CustomData is provided - JsonPathRequirement needs custom data Assert.Throws <ArgumentNullException>(() => equalsRequirement.Evaluate(context, resource)); }