Пример #1
0
 private ExpectationResult VerifyDoesNotHaveInstance(IPolicyContainer policyContainer, DoesNotHaveInstanceExpectation expectation)
 {
     if (expectation == null) return null;
     if (policyContainer != null && policyContainer.HasPolicy(expectation.Instance))
     {
         const string messageFormat = "An unexpected instance of the policy type \"{2}\" was found for controller \"{0}\", action \"{1}\".";
         var expectedPolicyType = expectation.Instance.GetType().Name;
         var message = string.Format(messageFormat, policyContainer.ControllerName, policyContainer.ActionName, expectedPolicyType);
         return _expectationViolationHandler.Handle(message);
     }
     return ExpectationResult.CreateSuccessResult();
 }
        public void Should_have_1_expecation_when_expecations_have_same_instance_for_has_and_does_not_have()
        {
            // Arrange
            var expectation1 = new HasInstanceExpectation(new RequireAnyRolePolicy("Editor"));
            var expectation2 = new DoesNotHaveInstanceExpectation(new RequireAnyRolePolicy("Editor"));

            // Act
            _expectationGroup.ApplyExpectation(expectation1);
            _expectationGroup.ApplyExpectation(expectation2);

            // Assert
            Assert.That(_expectationGroup.Expectations.Count(), Is.EqualTo(1));
        }
Пример #3
0
        private void HandleDoesNotHaveInstanceExpectation(DoesNotHaveInstanceExpectation expectation)
        {
            var hasInstanceExpectations = _expectations.Where(x => x is HasInstanceExpectation).Cast<HasInstanceExpectation>().Where(x => x.Instance.Equals(expectation.Instance));
            for (var i = 0; i < hasInstanceExpectations.Count(); i++)
            {
                var hasInstanceExpectation = hasInstanceExpectations.ElementAt(i);
                _expectations.Remove(hasInstanceExpectation);
            }

            var doesNotHaveTypeExpectations = _expectations.Where(x => x is DoesNotHaveInstanceExpectation).Cast<DoesNotHaveInstanceExpectation>();
            if (doesNotHaveTypeExpectations.Any(x => x.Instance.Equals(expectation.Instance))) return;

            _expectations.Add(expectation);
        }
        public void Should_have_2_expecation_when_expecations_have_different_instances_for_DoesNotHaveInstanceExpectation()
        {
            // Arrange
            var expectation1 = new DoesNotHaveInstanceExpectation(new RequireAnyRolePolicy("Editor"));
            var expectation2 = new DoesNotHaveInstanceExpectation(new RequireAnyRolePolicy("Editor", "Writer"));

            // Act
            _expectationGroup.ApplyExpectation(expectation1);
            _expectationGroup.ApplyExpectation(expectation2);

            // Assert
            Assert.That(_expectationGroup.Expectations.Count(), Is.EqualTo(2));
        }