Skip to content

Commit 1e77952

Browse files
jurajplavcanBillWagner
authored andcommitted
correcting tested method name to squaresOfOdds (dotnet#4904)
* correcting tested method name to squaresOfOdds * f# unittest - first test snippet updated: removed toList conversion
1 parent bf7dba6 commit 1e77952

File tree

3 files changed

+36
-38
lines changed

3 files changed

+36
-38
lines changed

docs/core/testing/unit-testing-fsharp-with-dotnet-test.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Make *MathService* the current directory and run [`dotnet new classlib -lang F#`
3232

3333
```fsharp
3434
module MyMath =
35-
let sumOfSquares xs = raise (System.NotImplementedException("You haven't written a test yet!"))
35+
let squaresOfOdds xs = raise (System.NotImplementedException("You haven't written a test yet!"))
3636
```
3737

3838
Change the directory back to the *unit-testing-with-fsharp* directory. Run [`dotnet sln add .\MathService\MathService.fsproj`](../tools/dotnet-sln.md)
@@ -99,22 +99,22 @@ let ``Fail every time`` () = Assert.True(false)
9999

100100
The `[<Fact>]` attribute denotes a test method that is run by the test runner. From the *unit-testing-with-fsharp*, execute [`dotnet test`](../tools/dotnet-test.md) to build the tests and the class library and then run the tests. The xUnit test runner contains the program entry point to run your tests. `dotnet test` starts the test runner using the unit test project you've created.
101101

102-
These two tests show the most basic passing and failing tests. `My test` passes, and `Fail every time` fails. Now, create a test for the `sumOfSquares` method. The `sumOfSquares` method returns the sum of the squares of all odd integer values that are part of the input sequence. Rather than trying to write all of those functions at once, you can iteratively create tests that validate the functionality. Making each test pass means creating the necessary functionality for the method.
102+
These two tests show the most basic passing and failing tests. `My test` passes, and `Fail every time` fails. Now, create a test for the `squaresOfOdds` method. The `squaresOfOdds` method returns a sequence of the squares of all odd integer values that are part of the input sequence. Rather than trying to write all of those functions at once, you can iteratively create tests that validate the functionality. Making each test pass means creating the necessary functionality for the method.
103103

104-
The simplest test we can write is to call `sumOfSquares` with all even numbers, where the result should be an empty sequence of integers. Here's that test:
104+
The simplest test we can write is to call `squaresOfOdds` with all even numbers, where the result should be an empty sequence of integers. Here's that test:
105105

106106
```fsharp
107107
[<Fact>]
108-
let ``Sum of evens returns empty collection`` () =
108+
let ``Sequence of Evens returns empty collection`` () =
109109
let expected = Seq.empty<int>
110-
let actual = MyMath.sumOfSquares [2; 4; 6; 8; 10]
110+
let actual = MyMath.squaresOfOdds [2; 4; 6; 8; 10]
111111
Assert.Equal<Collections.Generic.IEnumerable<int>>(expected, actual)
112112
```
113113

114114
Your test fails. You haven't created the implementation yet. Make this test by writing the simplest code in the `MathService` class that works:
115115

116116
```csharp
117-
let sumOfSquares xs =
117+
let squaresOfOdds xs =
118118
Seq.empty<int>
119119
```
120120

@@ -126,18 +126,18 @@ Now that you've made one test pass, it's time to write more. The next simple cas
126126

127127
```fsharp
128128
[<Fact>]
129-
let ``Sum of sequences of Ones and Evens`` () =
129+
let ``Sequences of Ones and Evens returns Ones`` () =
130130
let expected = [1; 1; 1; 1]
131-
let actual = MyMath.sumOfSquares [2; 1; 4; 1; 6; 1; 8; 1; 10]
131+
let actual = MyMath.squaresOfOdds [2; 1; 4; 1; 6; 1; 8; 1; 10]
132132
Assert.Equal<Collections.Generic.IEnumerable<int>>(expected, actual)
133133
```
134134

135-
Executing `dotnet test` runs your tests and shows you that the new test fails. Now, update the `sumOfSquares` method to handle this new test. You filter all the even numbers out of the sequence to make this test pass. You can do that by writing a small filter function and using `Seq.filter`:
135+
Executing `dotnet test` runs your tests and shows you that the new test fails. Now, update the `squaresOfOdds` method to handle this new test. You filter all the even numbers out of the sequence to make this test pass. You can do that by writing a small filter function and using `Seq.filter`:
136136

137137
```fsharp
138138
let private isOdd x = x % 2 <> 0
139139

140-
let sumOfSquares xs =
140+
let squaresOfOdds xs =
141141
xs
142142
|> Seq.filter isOdd
143143
```
@@ -148,7 +148,7 @@ There's one more step to go: square each of the odd numbers. Start by writing a
148148
[<Fact>]
149149
let ``SquaresOfOdds works`` () =
150150
let expected = [1; 9; 25; 49; 81]
151-
let actual = MyMath.sumOfSquares [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
151+
let actual = MyMath.squaresOfOdds [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
152152
Assert.Equal(expected, actual)
153153
```
154154

@@ -158,7 +158,7 @@ You can fix the test by piping the filtered sequence through a map operation to
158158
let private square x = x * x
159159
let private isOdd x = x % 2 <> 0
160160

161-
let sumOfSquares xs =
161+
let squaresOfOdds xs =
162162
xs
163163
|> Seq.filter isOdd
164164
|> Seq.map square

docs/core/testing/unit-testing-fsharp-with-mstest.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Make *MathService* the current directory and run [`dotnet new classlib -lang F#`
3232

3333
```fsharp
3434
module MyMath =
35-
let sumOfSquares xs = raise (System.NotImplementedException("You haven't written a test yet!"))
35+
let squaresOfOdds xs = raise (System.NotImplementedException("You haven't written a test yet!"))
3636
```
3737

3838
Change the directory back to the *unit-testing-with-fsharp* directory. Run [`dotnet sln add .\MathService\MathService.fsproj`](../tools/dotnet-sln.md)
@@ -108,15 +108,15 @@ type TestClass () =
108108

109109
The `[<TestClass>]` attribute denotes a class that contains tests. The `[<TestMethod>]` attribute denotes a test method that is run by the test runner. From the *unit-testing-with-fsharp* directory, execute [`dotnet test`](../tools/dotnet-test.md) to build the tests and the class library and then run the tests. The MSTest test runner contains the program entry point to run your tests. `dotnet test` starts the test runner using the unit test project you've created.
110110

111-
These two tests show the most basic passing and failing tests. `My test` passes, and `Fail every time` fails. Now, create a test for the `sumOfSquares` method. The `sumOfSquares` method returns the sum of the squares of all odd integer values that are part of the input sequence. Rather than trying to write all of those functions at once, you can iteratively create tests that validate the functionality. Making each test pass means creating the necessary functionality for the method.
111+
These two tests show the most basic passing and failing tests. `My test` passes, and `Fail every time` fails. Now, create a test for the `squaresOfOdds` method. The `squaresOfOdds` method returns a list of the squares of all odd integer values that are part of the input sequence. Rather than trying to write all of those functions at once, you can iteratively create tests that validate the functionality. Making each test pass means creating the necessary functionality for the method.
112112

113-
The simplest test we can write is to call `sumOfSquares` with all even numbers, where the result should be an empty sequence of integers. Here's that test:
113+
The simplest test we can write is to call `squaresOfOdds` with all even numbers, where the result should be an empty sequence of integers. Here's that test:
114114

115115
```fsharp
116116
[<TestMethod>]
117117
member this.TestEvenSequence() =
118118
let expected = Seq.empty<int> |> Seq.toList
119-
let actual = MyMath.sumOfSquares [2; 4; 6; 8; 10]
119+
let actual = MyMath.squaresOfOdds [2; 4; 6; 8; 10]
120120
Assert.AreEqual(expected, actual)
121121
```
122122

@@ -125,7 +125,7 @@ Notice that the `expected` sequence has been converted to a list. The MSTest lib
125125
When you run the test, you see that your test fails. You haven't created the implementation yet. Make this test by writing the simplest code in the `Mathservice` class that works:
126126

127127
```csharp
128-
let sumOfSquares xs =
128+
let squaresOfOdds xs =
129129
Seq.empty<int> |> Seq.toList
130130
```
131131

@@ -137,18 +137,18 @@ Now that you've made one test pass, it's time to write more. The next simple cas
137137

138138
```fsharp
139139
[<TestMethod>]
140-
member public this.SumOnesAndEvens() =
140+
member public this.TestOnesAndEvens() =
141141
let expected = [1; 1; 1; 1]
142-
let actual = MyMath.sumOfSquares [2; 1; 4; 1; 6; 1; 8; 1; 10]
142+
let actual = MyMath.squaresOfOdds [2; 1; 4; 1; 6; 1; 8; 1; 10]
143143
Assert.AreEqual(expected, actual)
144144
```
145145

146-
Executing `dotnet test` fails the new test. You must update the `sumOfSquares` method to handle this new test. You must filter all the even numbers out of the sequence to make this test pass. You can do that by writing a small filter function and using `Seq.filter`:
146+
Executing `dotnet test` fails the new test. You must update the `squaresOfOdds` method to handle this new test. You must filter all the even numbers out of the sequence to make this test pass. You can do that by writing a small filter function and using `Seq.filter`:
147147

148148
```fsharp
149149
let private isOdd x = x % 2 <> 0
150150

151-
let sumOfSquares xs =
151+
let squaresOfOdds xs =
152152
xs
153153
|> Seq.filter isOdd |> Seq.toList
154154
```
@@ -161,7 +161,7 @@ There's one more step to go: square each of the odd numbers. Start by writing a
161161
[<TestMethod>]
162162
member public this.TestSquaresOfOdds() =
163163
let expected = [1; 9; 25; 49; 81]
164-
let actual = MyMath.sumOfSquares [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
164+
let actual = MyMath.squaresOfOdds [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
165165
Assert.AreEqual(expected, actual)
166166
```
167167

@@ -171,7 +171,7 @@ You can fix the test by piping the filtered sequence through a map operation to
171171
let private square x = x * x
172172
let private isOdd x = x % 2 <> 0
173173

174-
let sumOfSquares xs =
174+
let squaresOfOdds xs =
175175
xs
176176
|> Seq.filter isOdd
177177
|> Seq.map square

docs/core/testing/unit-testing-fsharp-with-nunit.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Make *MathService* the current directory and run [`dotnet new classlib -lang F#`
2929

3030
```fsharp
3131
module MyMath =
32-
let sumOfSquares xs = raise (System.NotImplementedException("You haven't written a test yet!"))
32+
let squaresOfOdds xs = raise (System.NotImplementedException("You haven't written a test yet!"))
3333
```
3434

3535
Change the directory back to the *unit-testing-with-fsharp* directory. Run [`dotnet sln add .\MathService\MathService.fsproj`](../tools/dotnet-sln.md)
@@ -113,15 +113,15 @@ type TestClass () =
113113

114114
The `[<TestFixture>]` attribute denotes a class that contains tests. The `[<Test>]` attribute denotes a test method that is run by the test runner. From the *unit-testing-with-fsharp* directory, execute [`dotnet test`](../tools/dotnet-test.md) to build the tests and the class library and then run the tests. The NUnit test runner contains the program entry point to run your tests. `dotnet test` starts the test runner using the unit test project you've created.
115115

116-
These two tests show the most basic passing and failing tests. `My test` passes, and `Fail every time` fails. Now, create a test for the `sumOfSquares` method. The `sumOfSquares` method returns the sum of the squares of all odd integer values that are part of the input sequence. Rather than trying to write all of those functions at once, you can iteratively create tests that validate the functionality. Making each test pass means creating the necessary functionality for the method.
116+
These two tests show the most basic passing and failing tests. `My test` passes, and `Fail every time` fails. Now, create a test for the `squaresOfOdds` method. The `squaresOfOdds` method returns a sequence of the squares of all odd integer values that are part of the input sequence. Rather than trying to write all of those functions at once, you can iteratively create tests that validate the functionality. Making each test pass means creating the necessary functionality for the method.
117117

118-
The simplest test we can write is to call `sumOfSquares` with all even numbers, where the result should be an empty sequence of integers. Here's that test:
118+
The simplest test we can write is to call `squaresOfOdds` with all even numbers, where the result should be an empty sequence of integers. Here's that test:
119119

120120
```fsharp
121121
[<Test>]
122122
member this.TestEvenSequence() =
123-
let expected = Seq.empty<int> |> Seq.toList
124-
let actual = MyMath.sumOfSquares [2; 4; 6; 8; 10]
123+
let expected = Seq.empty<int>
124+
let actual = MyMath.squaresOfOdds [2; 4; 6; 8; 10]
125125
Assert.That(actual, Is.EqualTo(expected))
126126
```
127127

@@ -130,8 +130,8 @@ Notice that the `expected` sequence has been converted to a list. The NUnit fram
130130
When you run the test, you see that your test fails. You haven't created the implementation yet. Make this test by writing the simplest code in the `Mathservice` class that works:
131131

132132
```csharp
133-
let sumOfSquares xs =
134-
Seq.empty<int> |> Seq.toList
133+
let squaresOfOdds xs =
134+
Seq.empty<int>
135135
```
136136

137137
In the *unit-testing-with-fsharp* directory, run `dotnet test` again. The `dotnet test` command runs a build for the `MathService` project and then for the `MathService.Tests` project. After building both projects, it runs this single test. It passes.
@@ -142,21 +142,20 @@ Now that you've made one test pass, it's time to write more. The next simple cas
142142

143143
```fsharp
144144
[<Test>]
145-
member public this.SumOnesAndEvens() =
145+
member public this.TestOnesAndEvens() =
146146
let expected = [1; 1; 1; 1]
147-
let actual = MyMath.sumOfSquares [2; 1; 4; 1; 6; 1; 8; 1; 10]
147+
let actual = MyMath.squaresOfOdds [2; 1; 4; 1; 6; 1; 8; 1; 10]
148148
Assert.That(actual, Is.EqualTo(expected))
149149
```
150150

151-
Executing `dotnet test` fails the new test. You must update the `sumOfSquares` method to handle this new test. You must filter all the even numbers out of the sequence to make this test pass. You can do that by writing a small filter function and using `Seq.filter`:
151+
Executing `dotnet test` fails the new test. You must update the `squaresOfOdds` method to handle this new test. You must filter all the even numbers out of the sequence to make this test pass. You can do that by writing a small filter function and using `Seq.filter`:
152152

153153
```fsharp
154154
let private isOdd x = x % 2 <> 0
155155

156-
let sumOfSquares xs =
156+
let squaresOfOdds xs =
157157
xs
158158
|> Seq.filter isOdd
159-
|> Seq.toList
160159
```
161160

162161
Notice the call to `Seq.toList`. That creates a list, which implements the <xref:System.Collections.ICollection> interface.
@@ -167,7 +166,7 @@ There's one more step to go: square each of the odd numbers. Start by writing a
167166
[<Test>]
168167
member public this.TestSquaresOfOdds() =
169168
let expected = [1; 9; 25; 49; 81]
170-
let actual = MyMath.sumOfSquares [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
169+
let actual = MyMath.squaresOfOdds [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
171170
Assert.That(actual, Is.EqualTo(expected))
172171
```
173172

@@ -177,11 +176,10 @@ You can fix the test by piping the filtered sequence through a map operation to
177176
let private square x = x * x
178177
let private isOdd x = x % 2 <> 0
179178

180-
let sumOfSquares xs =
179+
let squaresOfOdds xs =
181180
xs
182181
|> Seq.filter isOdd
183182
|> Seq.map square
184-
|> Seq.toList
185183
```
186184

187185
You've built a small library and a set of unit tests for that library. You've structured the solution so that adding new packages and tests is part of the normal workflow. You've concentrated most of your time and effort on solving the goals of the application.

0 commit comments

Comments
 (0)