From 6277bd218390415f3598fc4b6414f6656188792b Mon Sep 17 00:00:00 2001 From: Dimitri Merejkowsky Date: Wed, 15 Feb 2023 11:55:26 +0100 Subject: [PATCH 1/3] cs: Extract Production code in a separate class Otherwise it's a bit hard to show what we're testing --- .../CurrencyConverter.Tests/Calculator.cs | 11 +++++++++++ .../Steps/CalculatorStepDefinitions.cs | 5 ++++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 csharp/CurrencyConverter/CurrencyConverter.Tests/Calculator.cs diff --git a/csharp/CurrencyConverter/CurrencyConverter.Tests/Calculator.cs b/csharp/CurrencyConverter/CurrencyConverter.Tests/Calculator.cs new file mode 100644 index 0000000..2ab1db3 --- /dev/null +++ b/csharp/CurrencyConverter/CurrencyConverter.Tests/Calculator.cs @@ -0,0 +1,11 @@ +namespace CurrencyConverter.Tests +{ + public class Calculator + { + public static int Add(int x, int y) + { + return x + y; + } + } + +} diff --git a/csharp/CurrencyConverter/CurrencyConverter.Tests/Steps/CalculatorStepDefinitions.cs b/csharp/CurrencyConverter/CurrencyConverter.Tests/Steps/CalculatorStepDefinitions.cs index d7cad3f..e890f19 100644 --- a/csharp/CurrencyConverter/CurrencyConverter.Tests/Steps/CalculatorStepDefinitions.cs +++ b/csharp/CurrencyConverter/CurrencyConverter.Tests/Steps/CalculatorStepDefinitions.cs @@ -33,7 +33,10 @@ public void WhenTheTwoNumbersAreAdded() { var first = _scenarioContext.Get("first"); var second = _scenarioContext.Get("second"); - _scenarioContext.Add("result", first + second); + + var sum = Calculator.Add(first, second); + + _scenarioContext.Add("result", sum); } [Then("the result should be (.*)")] From f7564c0654878d11cb023ae0384b2a5a2614fee2 Mon Sep 17 00:00:00 2001 From: Dimitri Merejkowsky Date: Wed, 15 Feb 2023 11:57:38 +0100 Subject: [PATCH 2/3] cs: add .gitignore, remove generated file from tracked files --- csharp/CurrencyConverter/.gitignore | 7 + .../Features/Calculator.feature.cs | 127 ------------------ 2 files changed, 7 insertions(+), 127 deletions(-) create mode 100644 csharp/CurrencyConverter/.gitignore delete mode 100644 csharp/CurrencyConverter/CurrencyConverter.Tests/Features/Calculator.feature.cs diff --git a/csharp/CurrencyConverter/.gitignore b/csharp/CurrencyConverter/.gitignore new file mode 100644 index 0000000..db31af8 --- /dev/null +++ b/csharp/CurrencyConverter/.gitignore @@ -0,0 +1,7 @@ +.vs/ +bin/ +obj/ + +CurrencyConverter.Tests/Features/Calculator.feature.cs + +TestResults/ \ No newline at end of file diff --git a/csharp/CurrencyConverter/CurrencyConverter.Tests/Features/Calculator.feature.cs b/csharp/CurrencyConverter/CurrencyConverter.Tests/Features/Calculator.feature.cs deleted file mode 100644 index c6f62c6..0000000 --- a/csharp/CurrencyConverter/CurrencyConverter.Tests/Features/Calculator.feature.cs +++ /dev/null @@ -1,127 +0,0 @@ -// ------------------------------------------------------------------------------ -// -// This code was generated by SpecFlow (https://www.specflow.org/). -// SpecFlow Version:3.9.0.0 -// SpecFlow Generator Version:3.9.0.0 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -// ------------------------------------------------------------------------------ -#region Designer generated code -#pragma warning disable -namespace CurrencyConverter.Tests.Features -{ - using TechTalk.SpecFlow; - using System; - using System.Linq; - - - [System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "3.9.0.0")] - [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [TechTalk.SpecRun.FeatureAttribute("Calculator", Description=@"![Calculator](https://specflow.org/wp-content/uploads/2020/09/calculator.png) -Simple calculator for adding **two** numbers - -Link to a feature: [Calculator](CurrencyConverter.Tests/Features/Calculator.feature) -***Further read***: **[Learn more about how to generate Living Documentation](https://docs.specflow.org/projects/specflow-livingdoc/en/latest/LivingDocGenerator/Generating-Documentation.html)**", SourceFile="Features\\Calculator.feature", SourceLine=0)] - public partial class CalculatorFeature - { - - private TechTalk.SpecFlow.ITestRunner testRunner; - - private string[] _featureTags = ((string[])(null)); - -#line 1 "Calculator.feature" -#line hidden - - [TechTalk.SpecRun.FeatureInitialize()] - public virtual void FeatureSetup() - { - testRunner = TechTalk.SpecFlow.TestRunnerManager.GetTestRunner(); - TechTalk.SpecFlow.FeatureInfo featureInfo = new TechTalk.SpecFlow.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "Features", "Calculator", @"![Calculator](https://specflow.org/wp-content/uploads/2020/09/calculator.png) -Simple calculator for adding **two** numbers - -Link to a feature: [Calculator](CurrencyConverter.Tests/Features/Calculator.feature) -***Further read***: **[Learn more about how to generate Living Documentation](https://docs.specflow.org/projects/specflow-livingdoc/en/latest/LivingDocGenerator/Generating-Documentation.html)**", ProgrammingLanguage.CSharp, ((string[])(null))); - testRunner.OnFeatureStart(featureInfo); - } - - [TechTalk.SpecRun.FeatureCleanup()] - public virtual void FeatureTearDown() - { - testRunner.OnFeatureEnd(); - testRunner = null; - } - - public virtual void TestInitialize() - { - } - - [TechTalk.SpecRun.ScenarioCleanup()] - public virtual void TestTearDown() - { - testRunner.OnScenarioEnd(); - } - - public virtual void ScenarioInitialize(TechTalk.SpecFlow.ScenarioInfo scenarioInfo) - { - testRunner.OnScenarioInitialize(scenarioInfo); - } - - public virtual void ScenarioStart() - { - testRunner.OnScenarioStart(); - } - - public virtual void ScenarioCleanup() - { - testRunner.CollectScenarioErrors(); - } - - [TechTalk.SpecRun.ScenarioAttribute("Add two numbers", new string[] { - "mytag"}, SourceLine=8)] - public virtual void AddTwoNumbers() - { - string[] tagsOfScenario = new string[] { - "mytag"}; - System.Collections.Specialized.OrderedDictionary argumentsOfScenario = new System.Collections.Specialized.OrderedDictionary(); - TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Add two numbers", null, tagsOfScenario, argumentsOfScenario, this._featureTags); -#line 9 -this.ScenarioInitialize(scenarioInfo); -#line hidden - bool isScenarioIgnored = default(bool); - bool isFeatureIgnored = default(bool); - if ((tagsOfScenario != null)) - { - isScenarioIgnored = tagsOfScenario.Where(__entry => __entry != null).Where(__entry => String.Equals(__entry, "ignore", StringComparison.CurrentCultureIgnoreCase)).Any(); - } - if ((this._featureTags != null)) - { - isFeatureIgnored = this._featureTags.Where(__entry => __entry != null).Where(__entry => String.Equals(__entry, "ignore", StringComparison.CurrentCultureIgnoreCase)).Any(); - } - if ((isScenarioIgnored || isFeatureIgnored)) - { - testRunner.SkipScenario(); - } - else - { - this.ScenarioStart(); -#line 10 - testRunner.Given("the first number is 50", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Given "); -#line hidden -#line 11 - testRunner.And("the second number is 70", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And "); -#line hidden -#line 12 - testRunner.When("the two numbers are added", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "When "); -#line hidden -#line 13 - testRunner.Then("the result should be 120", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Then "); -#line hidden - } - this.ScenarioCleanup(); - } - } -} -#pragma warning restore -#endregion From cf0c41fca2f1ccbac1c5ea289994918693b61d9c Mon Sep 17 00:00:00 2001 From: Dimitri Merejkowsky Date: Wed, 15 Feb 2023 11:58:42 +0100 Subject: [PATCH 3/3] Replace FluentAssertions with Shouldly Could not get the tests to fail otherwise :( --- .../CurrencyConverter.Tests/CurrencyConverter.Tests.csproj | 2 +- .../Steps/CalculatorStepDefinitions.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/csharp/CurrencyConverter/CurrencyConverter.Tests/CurrencyConverter.Tests.csproj b/csharp/CurrencyConverter/CurrencyConverter.Tests/CurrencyConverter.Tests.csproj index a6761d3..cc22af4 100644 --- a/csharp/CurrencyConverter/CurrencyConverter.Tests/CurrencyConverter.Tests.csproj +++ b/csharp/CurrencyConverter/CurrencyConverter.Tests/CurrencyConverter.Tests.csproj @@ -7,9 +7,9 @@ + - \ No newline at end of file diff --git a/csharp/CurrencyConverter/CurrencyConverter.Tests/Steps/CalculatorStepDefinitions.cs b/csharp/CurrencyConverter/CurrencyConverter.Tests/Steps/CalculatorStepDefinitions.cs index e890f19..2a58a82 100644 --- a/csharp/CurrencyConverter/CurrencyConverter.Tests/Steps/CalculatorStepDefinitions.cs +++ b/csharp/CurrencyConverter/CurrencyConverter.Tests/Steps/CalculatorStepDefinitions.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using Shouldly; using TechTalk.SpecFlow; namespace CurrencyConverter.Tests.Steps @@ -43,7 +43,7 @@ public void WhenTheTwoNumbersAreAdded() public void ThenTheResultShouldBe(int result) { var actualResult = _scenarioContext.Get("result"); - actualResult.Should().Equals(result); + actualResult.ShouldBe(result); } } }