.NET Core and C#
Lab 3
Let's start with quick knowledge refresher
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
Console.WriteLine(cars[0]);
#array length
Console.WriteLine(cars.Length);
# loop through array
foreach (string i in cars)
{
Console.WriteLine(i);
}
# sorting array
Array.Sort(myNumbers);
public class Temperature : IComparable // or generic ICompareble<Temperature>
{
// The temperature value
protected double temperatureF;
public int CompareTo(object obj) {
if (obj == null) return 1;
Temperature otherTemperature = obj as Temperature;
if (otherTemperature != null)
return this.temperatureF.CompareTo(otherTemperature.temperatureF);
else
throw new ArgumentException("Object is not a Temperature");
}
}
public readonly struct Fraction
{
public static Fraction operator /(Fraction a, Fraction b)
{
if (b.num == 0)
{
throw new DivideByZeroException();
}
return new Fraction(a.num * b.den, a.den * b.num);
}
public override string ToString() => $"{num} / {den}";
}
Writing unit tests
- to test our methods or small blocks of code
- independent tests ideally with mocked data
- xUnit, MSTest, NUnit
- boring job but ensuring you don't break software in long run
Writing unit tests
- add package xunit to project
dotnet add package xunit
- now you can run unit tests using
dotnet test
- in solution create new project as classlib
dotnet new classlib -o ProjectTests
dotnet sln add ProjectTests
To write tests
add usings
and start writing
- reference source project
dotnet add reference ../MyApp/MyApp.csproj
[Theory]
[InlineData(3)]
[InlineData(5)]
[InlineData(6)]
public void MyFirstTheory(int value)
{
Assert.True(IsOdd(value));
}
bool IsOdd(int value)
{
return value % 2 == 1;
}
To write tests
- or use Test attribute
[Fact]
public void MyFirstTheory(int value)
{
Assert.True(IsOdd(3));
}
bool IsOdd(int value)
{
return value % 2 == 1;
}
How to assert
[Test]
public void MyFirstTheory(int value)
{
Assert.True();
Assert.False();
Assert.Equal();
Assert.NotEqual();
Assert.Contains();
Assert.IsRange();
Assert.NotInRange();
Assert.Throws<T>();
}
slide was left intentionally blank
Today's task
Invoicing
Vytvořte solution Invoicing.
V ní projekt InvocingApp.
Vytvořte třídu Item
- item má propsy Description a Amount
Today's task
Invoicing
Vytvořte rozhraní IBill
- IBill má property Items, která obsahuje pole Item
- IBill má get property TotalAmount
Today's task
Invoicing
Vytvořte třídu Invoice, která implementuje IBill
- přidejte propsy Identifier, IssuedDate, AcceptanceDate, TaxDate
- implementujte TotalAmount
- implementujte operátory < >, které porovnají faktury podle data přijetí, pokud jsou shodné podle data vystavní a i to pokud je shodné tak podle identifikátoru. Menší je ta, co byla vydaná dříve, případně dříve zavedena do systému.
Today's task
Invoicing
Vytvořte třídu InvoicePrinter, která má metodu Print() přijímající IBill. Metoda Print() vytiskne všechny položku s délkou řádku 120. Na levé straně bude popis položky a na pravé částka.
Vytvořte unit testy testující Invoice a jeho TotalAmount, a InvoicePrinter jeho metodu Print().
.NET Core Lab 3
By Lukáš Grolig
.NET Core Lab 3
- 369