0

I am writing some integration tests for a c# .net core api we have using specflow. So far everything is going well but I have come across an issue that's driving me mad. I have the following couple of scenarios:

Scenario: Test 1
Given I am ready
When I update invoice 1 of customer 2

The method for this is defined as follows:

[When(@"I update invoice (.*) of customer (.*)]
public async Task UpdateInvoiceCustomer(int invoiceId, int customerId)......

The second scenario looks as follows:

Scenario: Test 1
Given I am ready
When I update invoice 1 of customer 2 to ready

The method for this is defined as follows:

[When(@"I update invoice (.*) of customer (.*) to (.*)]
public async Task UpdateInvoiceCustomerStatus(int invoiceId, int customerId, string status)......

The problem I have is that VS cannot distinguish between the two and keeps trying to run test 2 against test 1 c# code and failing as its trying to convert the string "1 to active" to a number. I think there must be a way to detect there are three parameters or to even inform specflow that the string contains int, int, string not int, int but I'm struggling. As an aside, I can get around this by rewording the string - I don't want to though as there must be a solution out there?

user1474992
  • 739
  • 3
  • 7
  • 18
  • You need two methods 1) UpdateInvoiceCustomerStatus(int id1, int id2){} 2) UpdateInvoiceCustomerStatus(int id1, int id2, string str){} – jdweng Apr 15 '21 at 08:30
  • I do have the two methods. Specflow can't decide between the two. – user1474992 Apr 15 '21 at 09:23
  • This is a Specflow issue. Can you call Specflow with different parameters in the two methods? – jdweng Apr 15 '21 at 09:45
  • The syntax highlighting is a little of in your question. Did you mean to omit the closing double quotes at the end of the `When(...)` attributes? – Greg Burghardt Apr 15 '21 at 22:18

1 Answers1

0

See Specflow step definitions - strings need to be enclosed in single quotes. So Your example should be:

[When(@"I update invoice (.*) of customer (.*) to '(.*)'"]
public async Task UpdateInvoiceCustomerStatus(int invoiceId, int customerId, string status)

And your Specflow invocation would be:

When I update invoice 1 of customer 2 to 'ready'
auburg
  • 1,373
  • 2
  • 12
  • 22
  • Hi Auburg, I have tried that but it doesn't appear to work. Specflow highlights the text "2 to 'status'" in the same colour and tries to map it to an integer. The line is also highlighted with the error "ambiguous steps:" as it can decide between (int, int) and (int, int, string) – user1474992 Apr 15 '21 at 08:58
  • Sometimes Specflow gets it's knickers in a twist : try deleting the Specflow generated file and / or comment out the first version of your step (the one with int, int params). Also try with a test project with the int,int string version on it's own to see if that works. You may also want to delete the cache in order to force Specflow to regenerate the steps (see https://stackoverflow.com/questions/17167820/specflow-error-force-regenerate-steps-possible) – auburg Apr 15 '21 at 09:25
  • @auburg The problem is the regex. This has nothing todo with the generated files. – Andreas Willich Apr 15 '21 at 12:26
  • @user1474992 Is the 'ready' fixed or could it be something else also ? i.e. When I update invoice 1 of customer 2 to ready. If it's variable, what are the possible values could it be ? – auburg Apr 15 '21 at 15:10