How to use PropertyDefinedInInterface_ShouldNotExist_WhenCastToObject method of NUnit.Framework.Constraints.Base class

Best Nunit code snippet using NUnit.Framework.Constraints.Base.PropertyDefinedInInterface_ShouldNotExist_WhenCastToObject

Run Nunit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

PropertyConstraintTypeHierarchyTests.cs

Source: PropertyConstraintTypeHierarchyTests.cs Github

copy
1// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
2
3using System;
4using System.Collections.Generic;
5
6namespace NUnit.Framework.Constraints
7{
8    [TestFixture(TestOf = typeof(PropertyConstraint))]
9    public class PropertyConstraintTypeHierarchyTests
10    {
11        [Test]
12        public void PropertyDefinedInDerivedClass_ShouldExist()
13        {
14            var sut = new PropertyConstraint(nameof(Derived.SomeProperty), new EqualConstraint(42));
15
16            var instance = (Base)new Derived();
17            var actual = sut.ApplyTo(instance);
18
19            Assert.That(actual, Has.Property(nameof(ConstraintResult.Status)).EqualTo(ConstraintStatus.Success));
20
21            
22            var existSut = new PropertyExistsConstraint(nameof(Derived.SomeProperty));
23            var actualExist = existSut.ApplyTo(instance);
24            Assert.That(actualExist.IsSuccess, Is.True);
25        }
26
27        class Base { }
28        class Derived : Base
29        {
30            public int SomeProperty { get; set; } = 42;
31        }
32
33
34        private int[] _array;
35        private PropertyConstraint _countPropertyConstraint;
36        private PropertyExistsConstraint _countPropertyExistsConstraint;
37
38        [SetUp]
39        public void BeforeEveryTest()
40        {
41            _array = new[] { 1, 2, 3 };
42            _countPropertyConstraint = new PropertyConstraint(nameof(IList<object>.Count), new EqualConstraint(_array.Length));
43            _countPropertyExistsConstraint = new PropertyExistsConstraint(nameof(IList<object>.Count));
44        }
45
46        [Test]
47        public void ExplicitlyImplementedProperty_ShouldNotExist_ViaImplementingType()
48        {
49            var ex = Assert.Throws<ArgumentException>(() => _countPropertyConstraint.ApplyTo(_array));
50
51            Assert.That(ex, Has.Message.StartWith("Property Count was not found on System.Int32[]"));
52
53            var actualExist = _countPropertyExistsConstraint.ApplyTo(_array);
54            Assert.That(actualExist.IsSuccess, Is.False);
55        }
56
57        [Test]
58        public void PropertyDefinedInInterface_ShouldNotExist_WhenCastToObject()
59        {
60            var ex = Assert.Throws<ArgumentException>(() => _countPropertyConstraint.ApplyTo((object)_array));
61
62            Assert.That(ex, Has.Message.StartWith("Property Count was not found on System.Int32[]"));
63
64            var actualExist = _countPropertyExistsConstraint.ApplyTo((object)_array);
65            Assert.That(actualExist.IsSuccess, Is.False);
66        }
67
68        [Test]
69        public void PropertyDefinedInInterface_ShouldExist_WhenCastToICollection()
70        {
71            var actual = _countPropertyConstraint.ApplyTo((ICollection<int>)_array);
72
73            Assert.That(actual, Has.Property(nameof(ConstraintResult.Status)).EqualTo(ConstraintStatus.Success));
74
75            var actualExist = _countPropertyExistsConstraint.ApplyTo((ICollection<int>)_array);
76            Assert.That(actualExist.IsSuccess, Is.True);
77        }
78
79        [Test]
80        public void PropertyDefinedInInterface_ShouldExist_WhenCastToIList()
81        {
82            var actual = _countPropertyConstraint.ApplyTo((IList<int>)_array);
83
84            Assert.That(actual, Has.Property(nameof(ConstraintResult.Status)).EqualTo(ConstraintStatus.Success));
85
86            var actualExist = _countPropertyExistsConstraint.ApplyTo((IList<int>)_array);
87            Assert.That(actualExist.IsSuccess, Is.True);
88        }
89    }
90}
91
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger PropertyDefinedInInterface_ShouldNotExist_WhenCastToObject code on LambdaTest Cloud Grid

Execute automation tests with PropertyDefinedInInterface_ShouldNotExist_WhenCastToObject on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)