C#從零開始_自學C#、Visual Studio實境秀 45/ Getting Started with C# Lesson 18、Getti...



{



{Interfaces like thinking or thought and classes are blueprints baseing on those thinking or thought ,objects or instances are products produced according to the blueprints. Namo Amitābha

interface 就是腦袋中的構想, class 是將其構想畫出來成藍圖, object/instance 就是依照這個藍圖去做出來的實物。 南無阿彌陀佛

Interfaces are just like uncles and aunts ,and the base class is parent. Although we could only have a parent but we could also have all of the love from our uncles and aunts ; however,we can not inherit there property like our parent though. 南無阿彌陀佛 Namo Amitābha

} http://bit.ly/2wn8rKj

Lesson 18 of 20 Testing Your Code http://bit.ly/2voXpbe

{

測試程式碼是程式設計中最重要且最容易被忽略的部分。勿以善小而不為:Testing the code you write is one of the most important and underappreciated practices in programming.

5:00 Unit Tests test individual methods, your most basic "unit" of code, for completeness and correctness.

1:47:00 重構 refactor

This type of test drives Test Driven Development, or TDD, and should be small and fast with descriptive names.

To be clear, unit tests aren't limited only to TDD, they can and should be added at any time during development.

Unit tests are a great way to prevent and catch bugs during feature development as well as general refactoring.

xunit testing framework

7:00 組態:單字想複詞-九陽神功第2招。

run these tests from the dotnet CLI

project.json file



10:30 Writing Your First Test 撰寫測試

ExtensionMethods 擴充方法

27:00 為測試單元取一個好懂明瞭的名字:Before writing a test to confirm the result of your extension method above there is something important to consider, naming. Good naming for your unit tests can make your code essentially self-documenting. This makes it easier for other developers on your project to know what exactly a test is supposed to do, which means they will know what exactly the "unit" of code is intended for just by the name. 他們可以僅僅藉由那個名字就可以知道這個程式碼的“單元”究竟想要做些什麼。

30:30 所以 unit test 是一個 project 它具體執行測試的是交由一個 class 去完成(實作、實現)。

You can now begin to construct a unit test by adding a new class, ExtensionMethodsPlusFiveShould, to your test project.

32:00 In order to use the Xunit framework, you'll need to add the Xunit using.

33:00 the [Fact] attribute :Each unit test you create needs to have the [Fact] attribute assigned to it in order for the test to be picked up by the test runner. Below is the shell for your unit test.

40:20 可見中括號[],用在 array 、 collection 還有 attribute (屬性)上。

42:00 Below is the shell for your unit test.

using System;

using Xunit;

public class ExtensionMethodsPlusFiveShould

{

[Fact]

public void ReturnFiveMoreThanInput()

{

//Arrange



//Act



//Assert

}

}

50:00 56:50 3A(A、A、A):

You'll notice the comments of Arrange, Act, and Assert which form the structure of a unit test. The Arrange section is where you will set up all of your variables, collections of data, etc. that will be used for the test.  Arrange 就類似事前預備,所謂的「備便」

The Act section is for calling the method you're testing with the data you created from the Arrange section.

Finally, you will Assert what you expect to happen. If the behavior of the method matches your expectation (as defined by Assert statements), the test will pass but if not, it will fail.

It's a good practice to put the comments in there while you're beginnning to write unit tests, but over time it will become second nature, so the comments will not be necessary.在開始編寫單元測試時,將註解放在那裡是一個很好的做法,但隨著時間的推移,習慣成自然,這些註解也就沒那麼必要了。

1:07:00 有很多斷言 (Assert)的方法可以用:Xunit, like most testing frameworks, has many built in assertion methods for you to use. You can see Assert.Equal used above, but there are many more assertions available to you that you can check out here. http://bit.ly/2v0R1nu

assertions

執行測試:Once the test is written you can run the test command from the dotnet CLI (dotnet test). This will produce output like the following.

1:16:00 dotnet CLI (dotnet test) http://bit.ly/2u5awOm

1:25:00 The above example shows how to create a unit test for an extension method, so how does this work for an instance method?

1:26:00 可見擴充方法一定是 static ,因為它要直接供被擴充的類別來用。擴充方法其實就是擴充某一類別它原來沒有的方法;擴充一個新的方法給那個類別。The concept is nearly identical, except you'll need to create an instance of the class where your method is. Let's move our PlusFive method to a different, non-static, class.

}

2:15:00 2:20:00 Getting Started with xUnit.net (desktop) http://bit.ly/2w8NS4X

{

在 Manage NuGet Package(管理 NuGet 套件)時,須先儲存專案!(就是錯誤訊息顯示的「未儲存解決方案……」的意思)



}

}


留言

熱門文章