Avatar of victoriaharry
victoriaharry
 asked on

c# linqToExcel covert to string objects

Hi Guys,

I have a NUnit test in which I'm trying to read the testdata from rows in an excel file. My NUnit test is quite simple for this example and is below

 [Test, TestCaseSource("linqTesting2")]
        public void CompleteFormTestDataSource4(String username, String password)
        {

            LoginPageObject pageLogin = new LoginPageObject();
            EAPageObject pageEA = pageLogin.Login(username, password);

        }

Open in new window


My method to read the excel rows using linqToExcel is below

 
public static IQueryable<LinqToExcel.Row> linqTesting2()
        {
            var excel = new ExcelQueryFactory(Constants.testDataPath4);
            var testcases = from c in excel.Worksheet("Sheet1")
                            where c["UserName"] == "SonjaB"
                            select c;
            return testcases;
        }

Open in new window


To use the row data in my unit test do I need to somehow convert it to a IEnumerable<string[]> before it gets returned to the test. If so any ideas on how to do this would be appreciated. When I ran the test the only error I get is I need to pass the correct amount of parameters.

Thanks
C#.NET ProgrammingProgramming

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
Karrtik Iyer

So you want all the column values of a excel row to be converted to an array of string?
victoriaharry

ASKER
Yes, so each row of data can be passed into the NUnit test
Karrtik Iyer

Something like below.
 var testcases = (from Row r in excel.Worksheet("Sheet1")
                where r["UserName"] == "SonjaB"
                select r.ToArray()).AsEnumerable();
            IEnumerable<String[]> values = (from cells in testcases
                select cells.Select(x => x.Value.ToString()).ToArray()).AsEnumerable();

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Karrtik Iyer

You can combine into a single statement as well, like below:
       IEnumerable<string[]> testcases = (from Row r in excel.Worksheet("Sheet1")
                            where r["UserName"] == "SonjaB"
                            select r).AsEnumerable().Select( x => x.ToArray().Select( y => y.Value.ToString()).ToArray());

Open in new window

kaufmed

Based on what I'm seeing in the documentation, you should be creating an anonymous object that has each piece of data expected by your parameters:

public static IEnumerable<object> linqTesting2()
{
    var excel = new ExcelQueryFactory(Constants.testDataPath4);
    var testcases = from c in excel.Worksheet("Sheet1")
                    where c["UserName"] == "SonjaB"
                    select new
                    {
                        username = c["UserName"].Value,
                        password = c["Password"].Value
                    };
                    
    return testcases;
}

Open in new window


It appears that you add the properties to the anonymous object in the order that the test method lists parameters. My guess is that NUnit uses reflection to loop over the object properties, but I left the property names the same as the parameter names (including casing) just in case.

You also don't need AsEnumerable, ToArray, or any other similar method. The result of a LINQ query is already IEnumerable, so just go with that. You will note that I changed the return type of the factory method.
victoriaharry

ASKER
Thanks for the responses guys. When I run the test in Test Explorer using either of the responses above I get the error "Not enough arguments provided, provide at least two arguments. When I build the solution I can see the test explorer is creating the correct amount of tests and I can see in test explorer the data next to the test is from the excel file rows. However when I ran one of the tests I get the error. The test I'm running is below

[Test, TestCaseSource("linqTesting2")]
        public void CompleteFormTestDataSource4(String username, String password)
        {

            LoginPageObject pageLogin = new LoginPageObject();
            EAPageObject pageEA = pageLogin.Login(username, password);

        } 

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
victoriaharry

ASKER
I just noticed the data being sent back to the test is {username = SonjaB, password = mypassword} instead of {SonjaB,myPassword}
ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.