SpecFlow Table.CreateInstance returns null

906 views Asked by At

I am using SpecFlow version 1.9.0 I have a POCO class

    public class MerchantContactDetails 
{
    public string Title;
    public string Firstname;
    public string Lastname;
    public string Companyposition;
    public string Phonecode;
    public string Phoneno;
    public string Mobilecode;
    public string Mobileno;
    public string Email;
 }

My Specflow feature is

Scenario: Sample contact
When I add a new contact information
| title | firstname | lastname | companyposition | phonecode | phoneno | mobilecode | mobileno | email                |
| Mr    | Grim      | Smith    | Director        | 1 (US)    | 12345   | 1 (US)     | 45678    | [email protected] |

And my step definitions has

[When(@"I add a new contact information")]
    public void WhenIAddANewContactInformation(Table table)
    {
       MerchantContactDetails ContactDetails = table.CreateInstance<MerchantContactDetails>();
    }

When the scenario is executed, I see ContactDetails is set to null. However object table has Rows count set 1 and I could access values explicitly by call

table.Rows[0]["title"] => this works fine

only table.CreateInstance(); returns null

Please can someone point me in the right direction..?

Best Regards

1

There are 1 answers

0
JSDeveloper On

Finally I was able to find a solution. Turning all the fileds into properties makes SpecFlow to fill the object. The class definition now looks

public class MerchantContactDetails
{
    public string Title{get;set;}
    public string Firstname{get;set;}
    public string Lastname{get;set;}
    public string Companyposition{get;set;}
    public string Phonecode{get;set;}
    public string Phoneno{get;set;}
    public string Mobilecode{get;set;}
    public string Mobileno{get;set;}
    public string Email{get;set;}
 }

@ Sam Holder, thanks for your suggestion. The names of the columns can be case-insensitive. May also contain spaces. For example in SpecFlow feature we may have First Name, and it perfectly matches the class property firstname.