This project is read-only.
1

Resolved

Setting decimal properties in "bootstrapped" classes throws an exception

description

//Given these classes:
public class ClassWithDecimalProp
{
    public decimal DecProp { get; set; }
}
public class SomeContainerClass
{
    public int Id { get; set; }
    public ClassWithDecimalProp Reference { get; set; }
}
// the following test fails in the latest version (4ef032bc846c):
    [Test]
    public void should_be_able_to_create_class_with_decimal_containing_reference()
    {
        Assert.IsNotNull(Fabricator.Generate<SomeContainerClass>());
    }
// a hack I applied (because I didn't have more time) was to add the following code
                if (propertyValue != null &&
                    property.PropertyType == typeof(decimal) &&
                    propertyValue.GetType() == typeof(int))
                {
                    propertyValue = (decimal) (int) propertyValue;
                }
// immediately before the following code in ItemBuilder<T>.PopulatePropertyValues
                property.SetValue(item, propertyValue, null);
                propertiesResolvedInIteration.Add(property.Name);
Again, I know this is a bad hack but it might be of help if someone needs this quickly.
As a side note: I'm not sure why, but casting the boxed value to decimal directly would throw a ClassCastException whereas casting to (int) (and therefore unboxing it) and afterwards to decimal works.

comments

JRoughan wrote Dec 16, 2010 at 11:18 PM

I ran this test against the latest release (v0.3.3.17737) and it passes. I haven't rolled back to confirm the error but it's possible this was fixed as part of the other work done on this release.

Thanks for picking it up. Please let me know if you still believe there's a problem so I can close or fix this issue.

JRoughan wrote Dec 16, 2010 at 11:18 PM

Oh, and your test has been added to the test suite.