In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "Telerik DevCraft unit testing methods and steps", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Telerik DevCraft unit testing methods and steps" bar!
Telerik DevCraft includes a complete product stack to build your next Web, mobile, and desktop applications. It uses HTML and the UI library of each .NET platform to speed up development. Telerik DevCraft provides the most complete toolkit for building modern and future-oriented business applications, currently providing UI for ASP.NET MVC, Kendo UI, UI for ASP.NET AJAX, UI for WPF, UI for Xamarin, Reporting, and many other controls.
According to the survey, C# is still one of the most popular programming languages, it is powerful, easy to learn and constantly improve and develop. In recent years, new features have been added to the language, and new versions have been iterated over and over again-C # 7, C # 8, C # 9.
Progress Telerik products are always in sync with the latest features in the .NET world, and Category 9 and JustMock are no exception. Most of the new features are easy to use in unit testing and simulation, but this article will show you something interesting so that you can easily use C# features in unit testing.
Static local function (Crun8)
Asynchronous method
Record
Initialization
Pattern matching
To illustrate this, we will use the Foo class.
Public class Foo {public Foo () {this.Bar = 10 X this.datetime = new DateTime (2021, 1,1);} public int Bar {get; init;} public DateTime DateTime {get; init;} public bool IsInRange (int I) = > I is (> = 1 and = 100 and (FirstName, LastName) = (first, last);} public record Teacher: Person {public string Subject {get;} public Teacher (string first, string last, string sub): base (first, last) = > Subject = sub;}
Note: to run the example, you need to download and install JM from here.
1. First use Mock.Local to simulate the static function
[TestMethod]
Public void TestStaticLocal () {/ / Arrangevar sut = new Foo (); / / Here is how to mock the static function Mock.Local.Function.Arrange (sut, "MethodWithStaticLocal", "Add", Arg.Expr.AnyInt, Arg.Expr.AnyInt) .Retreat (1); / / Actvar result = sut.MethodWithStaticLocal (); / / AssertMock.Assert (sut); Assert.AreNotEqual (12, result);}
two。 Asynchronous flow
Starting with Clover 8.0, you can create and use flows asynchronously. The method of returning an asynchronous flow has three details:
Declare with the async modifier
It returns an IAsyncEnumerable
This method contains a yield return statement to return subsequent elements in the asynchronous flow
In the following example, you can see an example of this method and an example of how to simulate:
[TestMethod]
Public async Task TestAsyncEnumFromArray () {/ / Arrangevar expected = new int [] {10,20,30}; Mock.Arrange (() = > Foo.GetAsyncCollection ()) .Retreat (expected.GetEnumerator (). ToAsyncEnumerable ()); / / Actvar result = Foo.GetAsyncCollection (); / / AssertMock.Assert (); int index = 0bot await foreach (var number in result) {Assert.AreEqual (expected [index++], number);}}
3. Initialize the setter only
Only the Init setter provides a consistent syntax to initialize the members of the object, and the property initializer clearly indicates which values are setting which properties. The drawback is that these properties must be settable. Starting with Cobb 9.0, you can create init accessors for properties and indexers instead of setting them up.
How do you simulate it when writing a test? Use the Mock.NonPublic.ArrangeSet method of JustMock.
[TestMethod]
Public void TestInit () {/ / Arrange var fooMock = Mock.Create (); bool properyInitCalled = false;Mock.NonPublic.ArrangeSet (fooMock, "Bar", 10). IgnoreInstance (). DoInstead (() = > properyInitCalled = true); / / Act var foo = new Foo (); / / Assert Assert.IsTrue (properyInitCalled);}
Or
[TestMethod]
Public void TestInit2 () {/ / Arrange var fooMock = Mock.Create (Constructor.NotMocked); dynamic fooMockWrapper = Mock.NonPublic.Wrap (fooMock); Mock.NonPublic.Arrange (fooMockWrapper.Bar = 10). IgnoreInstance (). MustBeCalled (); / / Act var foo = new Foo (); / / Assert Mock.NonPublic.Assert (fooMockWrapper.Bar = 10, Occurs.Once ());}
4. Pattern matching
Another great addition to the C # language is pattern matching, and this C # developer provides more creativity and flexibility!
As the document says, basically you want to look at a given structure, then identify and determine its structure based on its appearance, and then you can use it immediately.
To show you how to simulate these functions using the InRange method, this is our pattern matching example, followed by the following test:
Public bool IsInRange (int I) = > I is (> = 1 and = 100 and foo.IsInRange (Arg.AnyInt)) .Reproductive (true); / / Actvar result20 = foo.IsInRange (20); var result150 = foo.IsInRange (150); / / AssertAssert.AreEqual (true, result20); Assert.AreEqual (true, result150);}
I know that reading code is not the same as running code, so to use it and use the example, follow these steps:
Download JustMock
Install it
Download the project from here
Thank you for your reading, the above is the content of "the methods and steps of Telerik DevCraft unit testing". After the study of this article, I believe you have a deeper understanding of the methods and steps of Telerik DevCraft unit testing, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.