'How can I manually / programmatically create a DataRow?
My project's codebase has a legacy method that takes a DataRow as a parameter for which I would like to create a unit test method.
How can I manually create a DataRow object to pass to the method? The System.Data.DataRow class doesn't have a public-facing constructor.
Solution 1:[1]
You should note that if the Unit Test needed to enforce type constraints on the DataColumnCollection (Columns) for the DataTable - you can use the overloaded constructor of the DataColumn class to include the expected Type.
var dt = new DataTable();
var dc = new DataColumn("Age", typeof(int));
dt.Columns.Add(dc);
var dr = dt.NewRow();
dr["Age"] = "test"; // throws an ArgumentException
//Input string was not in a correct format. Couldn't store<test> in Age Column. Expected type is Int32.
//should it succeed
dt.Rows.Add(dr);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
