AutoFixture > Using [Frozen] attribute on test method parameter

[Frozen] attribute is used to freeze instances of objects. It is applied on test method parameters.
We will consider simple examples with string comparison test.

Without [Frozen] attribute

using Ploeh.AutoFixture.Xunit2;
using Xunit;

[Theory, InlineAutoData]
public void No_Frozen_Attribute_Generates_Random_Strings(
	string one,
	string two)
{
	Assert.Equal(one, two);
}

Result: Failed

Using [Frozen] attribute

Applying [Frozen] on the first parameter of type string, will return the same values for all subsequent parameters.

[Theory, InlineAutoData]
public void Frozen_On_Parameter_Name_Freeze_Object_Instance(
   [Frozen]string one,
	string two)
{
	Assert.Equal(one, two);
}

Result: Passed

Using [Frozen] attribute – Order is important!

Applying [Frozen] attribute on the second parameter will generate two random strings.
But if we have third parameter of type ‘string’, then it will have the same value as ‘two’.

[Theory, InlineAutoData]
public void Frozen_On_Parameter_Name_Order_Is_Important(
	string one,
	[Frozen]string two)
{
	Assert.Equal(one, two);
}

Result: Failed

This entry was posted in Unit test and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s