I was getting the following error when running my tests using SpecFlow:
System.FormatException: Input string was not in a correct format.
And it took me a while to work out why it was happening.
I was getting the following error when running my tests using SpecFlow:
System.FormatException: Input string was not in a correct format.
And it took me a while to work out why it was happening.
It ended up being because I had omitted the single quotes in one of my step definitions, for example:
[Then(@"Something adds up to a quantity of (.*)")]
when it should have been
[Then(@"Something adds up to a quantity of '(.*)'")]
Note the single quotes around (.*)
Another thing to look for when running into this problem is to verify parameters.
When I update the thing by Id (<Id>)
Examples:
| Name |
| 1 |
The problem here is that "Id" does not match "Name" in the examples table
This may be INDEED a very time consuming issue, particulary when debugging step-by-step and only getting the System.FormatException: Input string was not in a correct format
when we get thru the last scenario line, although the offending binding code was related to a prior one.
In my case, I had initialy created a feature with several integers, which generated feature StepDefinitions code using INT parameters, and they runned fine. However, later, I've decide to test an edge case with a decimal value, but I forgot to revise feature StepDefinitions code according, namelly changing some parameters from INT to DECIMAL. That's when I run into troubles.
Feature: Calculator
Finds de differente between two numbers
Scenario: Subtract two numbers
Given the first number is 12.5
And the second number is 2.5
When the two numbers are subtracted
Then the result should be 10
using System;
using TechTalk.SpecFlow;
namespace ProcessamentoTests.StepDefinitions
{
[Binding]
public class CalculatorStepDefinitions
{
[Given(@"the first number is (.*)")]
public void GivenTheFirstNumberIs(decimal p0)
{
// Ignore for now
}
[Given(@"the second number is (.*)")]
public void GivenTheSecondNumberIs(int p0)
{
// Ignore for now
}
[When(@"the two numbers are subtracted")]
public void WhenTheTwoNumbersAreSubtracted()
{
// Ignore for now
}
[Then(@"the result should be (.*)")]
public void ThenTheResultShouldBe(int p0)
{
// Ignore for now
}
}
}
In this case, both p0 parameters on GivenTheSecondNumberIs and ThenTheResultShouldBe should be changed do DECIMAL type.
Therefore, until Specflow's team (*) manages a way to provide better context on parameter parsing issues, all scenario parameter bindings should be revised.
(*) Bear in mind that, given Specflow is a beloved open source project, everyone (including me) may be part of its team.
© 2022 - 2024 — McMap. All rights reserved.