mardi 30 juin 2015

Dealing with HttpResponse in unit tests

I have a simple website that has one page. The function I am unit testing is

public void WriteFileToClient(string fileContent, string fileName)
        {
            StringWriter stringWriter = new StringWriter();
            stringWriter.Write(fileContent);
            Response.ContentType = "text/plain";
            Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.txt", fileName));
            Response.Clear();

            using (StreamWriter writer = new StreamWriter(Response.OutputStream, Encoding.UTF8))
            {
                writer.Write(stringWriter.ToString());
            }
            Response.End();
        }

When I run my test it says that Response is not available enter image description here

Here is my Unit test

[TestMethod]
        [TestCategory("Unit")]
        public void WriteFileToClient_ShouldSucceedForCorrectInput()
        {
            //Arrange
            Migrate migrate = new Migrate();
            Guid g = Guid.Parse("fffe49c1-a838-46bc-b5c6-4e0bbd3e4c32");

            string fileContent = migrate.GenerateFlatFileText(g);
            string fileName = string.Format("flat file - {0}.txt", g);

            //Ask
            migrate.WriteFileToClient(fileContent, fileName);

            //Assert
            migrate.Response.OutputStream.Should().NotBeNull();
            migrate.Response.ContentType.Should().Be("text/plain");
            migrate.Response.Headers.Should().HaveCount(1);
        }

Any suggestions on how to mock the response? honestly I don't understand why the Response object is not available, my Migrate class inherits Page and to my understanding should has the Response included

Aucun commentaire:

Enregistrer un commentaire