SpecFlow.VisualStudio icon indicating copy to clipboard operation
SpecFlow.VisualStudio copied to clipboard

Misleading "Multiple matching bindings" message when step is bound to another feature

Open lucjanl opened this issue 9 years ago • 9 comments

I use SpecFlow 2.0 and SpecFlow for VisualStudio 2015 version 2015.1.2. I wrote a feature:

Feature: Service selection - package selection
    In order to choose package
    As a receptionist
    I want to be define package requirement details

Background: 
    Given now is "2016-04-24 18:00"
    And hotel day starts at 14:00 and ends at 12:00

and steps definition ServiceSelectionTypeChoosingSteps.cs:

    [Scope(Feature = "Service selection type choosing")]
    [Scope(Feature = "Service selection - package selection")]
    [Scope(Feature = "Service selection defaults")]
    [Binding]
    public class ServiceSelectionTypeChoosingSteps
    {
        [Given(@"hotel day starts at (\d+):00 and ends at (\d+):00")]
        public void GivenHotelDayStartsAtAndEndsAt(Int32 startHour, Int32 endHour)
        {
            _mX.SetupGet(x => x.Night).Returns(new Night(startHour, endHour));
        }
...

Then I wrote another feature:

Feature: DateTimeExtensions - AddHotelDuration
    In order to ease use of dates
    As a programmes
    I want to be able to add duration to a given date

Scenario Outline: Add duration in minutes to given date
    Given hotel day starts at 14:00 and ends at 12:00

and step definition:

    [Binding]
    [Scope(Feature = "DateTimeExtensions - AddHotelDuration")]
    public class DateTimeExtensions_AddHotelDurationSteps
    {
        [Given(@"hotel day starts at (/d+):00 and ends at (/d+):00")]
        public void GivenHotelDayStartsAtAndEndsAt(Int32 startHour, Int32 endHour)
        {
            _night = new Night(startHour, endHour);
        }

Now in the first feature the line And hotel day starts at 14:00 and ends at 12:00 is black. In the second feature the line Given hotel day starts at 14:00 and ends at 12:00 is pink and when I select it and press F12, the SpecFlow addon tells me:

Multiple matching bindings found. Navigating to the first match...
ServiceSelectionTypeChoosingSteps.GivenHotelDayStartsAtAndEndsAt(Int32, Int32)

I suppose the feature scoping doesn't work. Am I right or I did something wrong?

lucjanl avatar May 12 '16 10:05 lucjanl

After submitting this issue I saw it was my mistake: I used slash ((/d+)) in the second step regex instead of backslash ((\d+)). But ultimately the message "Multiple matching bindings" is misleading...

lucjanl avatar May 12 '16 10:05 lucjanl

This is unrelated to this issue, but reading this issue reminded me a video I recorded many years ago.... "SpecFlow Anti-Pattern: Using Private Members to Retain State Between Steps " https://www.youtube.com/watch?v=IGvxMPX55vE

darrencauthon avatar May 12 '16 15:05 darrencauthon

I agree with the anti pattern assertion, but buyer beware, in the new parallel test world you don't want to be using ScenarioContext.Current but you should be requesting a ScenarioContext instance (or some other specific context class you've written) through the step class constructor

samholder avatar May 12 '16 15:05 samholder

@samholder That is correct... I'm going to have to record a new video!!!! :)

darrencauthon avatar May 12 '16 16:05 darrencauthon

@lucenty this is caused by the visual studio integration component reads the code in a different way from the runtime component of SpecFlow. As of now, this is difficult to solve in a general way. You might want to look at the visual studio extension code and see if there is a simple fix to solve this particular issue.

An alternate approach would be that if the integration component detects multiple matches, it could show a list similar to the one shown when a step definition is used in multiple scenarios.

dirkrombauts avatar May 12 '16 16:05 dirkrombauts

The issue is still open and occured for me too.

StarWars999123 avatar Aug 10 '18 15:08 StarWars999123

Hey guys, Specflow is binding two different sentences to the same method -

Test scenario - image

Method name - image

Also, "as draft" is highlighted as a parameter, although it is not.

Do you know where I'm making mistake?

vukdanilovic avatar Jul 11 '19 11:07 vukdanilovic

The problem is your Regex. (.*) catches nearly everything. In you case it could help, if you add ' (single quotes) around your parameters.

SabotageAndi avatar Jul 11 '19 12:07 SabotageAndi

That solved my problem. Thanks for help and fast answer!

vukdanilovic avatar Jul 12 '19 11:07 vukdanilovic