Reputation: 53
I'm working on a project and I have to Unit Test a Factory Design pattern in C#. Currently im stuck and I have no idea what I have too do. Can someone help me out? I want to Unit Test this code:
public class CinemaFactory : AbstractFactory //inheritance of AbstractFactory
{
public override Space createspace(AddItemsInProps item)
{
//returns new Cinema
return new Cinema(item.AreaType, item.Position, item.Dimension);
}
}
I also made a Unit test project and created this:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
//Arrange
Game1.Design_Patterns.CinemaFactory cinemaFactory = new Game1.Design_Patterns.CinemaFactory();
//Act
//Assert
}
}
Cinema:
public class Cinema : Space //inheritance of Space class
{
//Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods.
public Cinema(string areaType, string position, string dimension) : base(areaType, position, dimension)
{
//The sprite of the Cinema
this.Img = "cinema";
}
public void StartMovie()
{
}
}
This is de class AddItemsInProps:
public class AddItemsInProps
{
//Get: The get { } implementation must include a return statement. It can access any member on the class.
//Set: The set { } implementation receives the implicit argument "value." This is the value to which the property is assigned.
public string Classification { get; set; }
public string AreaType { get; set; }
public string Position { get; set; }
public string Dimension { get; set; }
public int Capacity { get; set; }
public int ID { get; set; }
}
}
This is de class Space:
public abstract class Space
{
public string AreaType { get; set; }
public Point Position { get; set; }
public Point Dimension { get; set; }
public int ID { get; set; }
public string Img { get; set; }
public int Afstand { get; set; }
public Space Vorige { get; set; }
public Dictionary<Space, int> Neighbours = new Dictionary<Space, int>();
This is de class AbstractFactory:
public abstract class AbstractFactory
{
//Creating Object
public abstract Space createspace(AddItemsInProps item);
}
Upvotes: 5
Views: 10387
Reputation: 17979
There are a few things you can test, such as the type of the object returned by the factory and also if the object is equivalent to an expected object.
An example - testing the equivalence:
[TestClass]
public class CinemaFactoryTests
{
[TestMethod]
public void GivenItemProps_WhenCreatingSpace_ShouldReturnExpectedSpace()
{
// arrange
var factory = new CinemaFactory();
var props = new AddItemsInProps
{
AreaType = "my area",
Position = "my position",
Dimension = "my dimension"
};
// act
Space cinema = factory.createspace(props);
// assert
var expectedCinema = new Cinema(props.AreaType, props.Position, props.Dimension);
cinema.Should().BeEquivalentTo(expectedCinema);
}
}
In this case I'm checking if the object returned by the factory is equivalent to the one expected.
PS: I'm using FluentAssertions to check if the objects are equivalent.
Upvotes: 1
Reputation: 247018
Create an instance of the subject under test and provide the necessary dependencies.
[TestClass]
public class CinemaFactorTests {
[TestMethod]
public void CinemaFactory_Should_Create_Cinema() {
//Arrange
var cinemaFactory = new Game1.Design_Patterns.CinemaFactory();
var item = new AddItemsInProps {
//set necessary properties here
AreaType = "value here",
Position = "value here",
Dimension = "value here"
};
//Act
var actual = cinemaFactory.createspace(item);
//Assert
Assert.IsNotNull(actual);
Assert.IsInstanceOfType(actual, typeof(Cinema));
//...you can also compare property values from item and actual for equality
Assert.AreEqual(item.AreaType, actual.AreaType);
//...
}
}
Invoke the method under test and then verify the expected behavior with the actual behavior
Upvotes: 6