As a BDD and MSpec beginner I am still not so sure about best practises and good habits related to BDD in general and specifically to MSpec.
Can the following example be improved? Does it follow best practises and good habits?
- Is the naming of my spec classes and behavior OK?
- Should I use behaviors in this scenario or should I use a common base class for the spec classes ?
- Is it ok to not have an
Establish
here? - Should I use static factory methods (
TestData
methods) to get test data or should the data be created in the spec itself? - Instead of testing each property in the behavior I could use
result.Equals()
but then I would test two things, which is not good, right?
Please feel free to refactor the example to something that you would say is better.
[Subject(typeof(DataItemReader))]
public class When_reading_a_DataItem_from_stream
{
Because of = () =>
{
using (var reader = new DataItemReader(
new MemoryStream(TestData.GetNormalDataItemAsByteArray()), Encryption.None))
{
result = reader.ReadItem();
}
};
Behaves_like<DataItemReader_that_reads_correctly> behavior;
protected static DataItem result;
}
[Subject(typeof(DataItemReader))]
public class When_reading_a_DataItem_from_encrypted_stream
{
Because of = () =>
{
using (var reader = new DataItemReader(
new MemoryStream(TestData.GetNormalDataItemAsByteArrayEncyrpted()), Encryption.Default))
{
result = reader.ReadItem();
}
};
Behaves_like<DataItemReader_that_reads_correctly> behavior;
protected static DataItem result;
}
[Behaviors]
public class DataItemReader_that_reads_correctly
{
protected static DataItem result;
It should_read_the_correct_DataItem = () =>
{
var testItem = TestData.GetNormalDataItem();
result.Property1.ShouldEqual(testItem.Property1);
result.Property2.ShouldEqual(testItem.Property2);
result.Property3.ShouldEqual(testItem.Property3);
};
}