user8075070
user8075070

Reputation:

SpecFlow - StepArgumentTransformation System.InvalidCastException: 'Object must implement IConvertible.'

I am having this issue in which I cant resolve. I am able to use tables fine in my tests but when I tried to use StepArgumentTransformation, now I am getting System.InvalidCastException: 'Object must implement IConvertible.' when i try to run the test

Here are my classes and feature

Feature: Test

Scenario Outline: Create a Test
    Given I Log into ISEOffice with "<Role>"
        | TestName| TestType| AdditonalTestType|
        | Name1     | Type1 | AddType1         |  
        | Name2     | Type2 | AddType2         |
        | Name3     | Type3 | AddType3         | 
    When I create Something  
    Then something should happen                      

    @Dev
    Examples:
 | UserRole    |
 | TestRole    |
    public class TestContext
    {
        public string TestName{ get; set; }

        public string TestType { get; set; }

        public string AdditionalTestType { get; set; }
    }
 public class TestTestData
    {
        public string TestName{ get; set; }

        public string TestType { get; set; }

        public string AdditionalTestType { get; set; }
    }
    [Binding]
    public class TestTransforms
    {
        [StepArgumentTransformation]

        public IEnumerable<TestTestData> TestTransform(Table table)
        {
            return table.CreateSet<TestTestData>();
        }

    }

    [Binding]
    public class NewTestSteps : Base
    {
        private readonly TestContext _testContext;

        public NewTestSteps(TestContext testContext)
        {
            _testContext = testContext;
        }

        [Given(@"I Log into Test App with ""(.*)""")]
        public void GivenILogIntoTestAppWith(string UserRole, IEnumerable<TestTestData> table)
        {
               // using context injection, how do I apply it here with the table data?
        }

I am having an issue at this part and when i try to run my code I get System.InvalidCastException: 'Object must implement IConvertible.'

Without StepArgumentTransformation i would apply the below and it worked fine. I suspect its to do with using IEnumerable but i am unable to find a solution

[Given(@"I Log into Test App with ""(.*)""")]
        public void GivenILogIntoTestAppWith(string UserRole, Table table)
        {
           var testData = table.CreateSet<TestTestData>();
           foreach (var data in testData)
               {
                // data.TestName etc.
               }
         }

Upvotes: 2

Views: 344

Answers (1)

Oleg Nikolaev
Oleg Nikolaev

Reputation: 101

I had the same problem with my code, and I fixed it by adding the missing [StepArgumentTransformation] attribute to the method.

I see that you already have yours, but double-check that you are using the TechTalk.SpecFlow namespace.

Upvotes: 0

Related Questions