vendredi 25 septembre 2015

Use XML stored in a resource file as mock data for unit tests

I'm trying to use some XML data stored in a resource file as test data for unit tests.

I have this code that takes the test data, converts it to a stream, and tries to deserialize it into a collection of Students that I can use with my mock dbset.

var stream = TestData.Students.ToStream();
var reader = new StreamReader(stream);
var serializer = new XmlSerializer(typeof(Collection<Student>));
_students = (Collection<Student>)serializer.Deserialize(reader);

It fails on the last line with this exception:

{"<Students xmlns=''> was not expected."}

I've tried adding the Serializable attribute to the Student class, tried adding the XMLRoot attribute to it, also searched around on Google for a while, but couldn't really find anything dealing with resource files.

On a side note, is there an easier/better method for getting test data?

Here's my XML data in the resource file.

<Students>
  <Student>
    <Id>1</Id>
    <LastName>Alexander</LastName>
    <FirstName>Carson</FirstName>
    <EnrollmentDate>2010-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>2</Id>
    <LastName>Alonso</LastName>
    <FirstName>Meredith</FirstName>
    <EnrollmentDate>2012-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>3</Id>
    <LastName>Anand</LastName>
    <FirstName>Arturo</FirstName>
    <EnrollmentDate>2013-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>4</Id>
    <LastName>Barzdukas</LastName>
    <FirstName>Gytis</FirstName>
    <EnrollmentDate>2012-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>5</Id>
    <LastName>Li</LastName>
    <FirstName>Yan</FirstName>
    <EnrollmentDate>2012-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>6</Id>
    <LastName>Justice</LastName>
    <FirstName>Peggy</FirstName>
    <EnrollmentDate>2011-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>7</Id>
    <LastName>Norman</LastName>
    <FirstName>Laura</FirstName>
    <EnrollmentDate>2013-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>8</Id>
    <LastName>Olivetto</LastName>
    <FirstName>Nino</FirstName>
    <EnrollmentDate>2005-08-11T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>9</Id>
    <LastName>Alexander</LastName>
    <FirstName>Carson</FirstName>
    <EnrollmentDate>2010-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>10</Id>
    <LastName>Alonso</LastName>
    <FirstName>Meredith</FirstName>
    <EnrollmentDate>2012-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>11</Id>
    <LastName>Anand</LastName>
    <FirstName>Arturo</FirstName>
    <EnrollmentDate>2013-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>12</Id>
    <LastName>Barzdukas</LastName>
    <FirstName>Gytis</FirstName>
    <EnrollmentDate>2012-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>13</Id>
    <LastName>Li</LastName>
    <FirstName>Yan</FirstName>
    <EnrollmentDate>2012-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>14</Id>
    <LastName>Justice</LastName>
    <FirstName>Peggy</FirstName>
    <EnrollmentDate>2011-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>15</Id>
    <LastName>Norman</LastName>
    <FirstName>Laura</FirstName>
    <EnrollmentDate>2013-09-01T00:00:00</EnrollmentDate>
  </Student>
  <Student>
    <Id>16</Id>
    <LastName>Olivetto</LastName>
    <FirstName>Nino</FirstName>
    <EnrollmentDate>2005-08-11T00:00:00</EnrollmentDate>
  </Student>
</Students>

Aucun commentaire:

Enregistrer un commentaire