I know this has been asked before, and invariably someone comes along and says "That's not a unit test, that's an integration test." I'm just going call bullshit on that.
So Lets consider a really simple entity and its map:
public class Tag
{
public virtual long Id { get; set; }
public virtual string Text { get; set; }
}
public class TagConfiguration : EntityTypeConfiguration<Tag>
{
public TagConfiguration()
{
ToTable("Tag", "dbo")
.HasKey(tag => tag.Id);
Property(tag => tag.Id)
.HasColumnType(SqlDbType.BigInt.ToString())
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(tag => tag.Text)
.HasColumnType(SqlDbType.NVarChar.ToString())
.HasMaxLength(64)
.IsRequired();
}
}
Basically we are looking at an entity that is a number keyed to a string.
What is it I want to test here? Well I don't really see why I shouldn't be able to Create an instance of TagConfiguration and make a few assertions about the object.
- Is the Table name correct?
- Is the Schema name correct?
- Are the long fields mapped to a SQL BigInt and not an SQL Int?
- Are the long fields going to a NOT NULL column?
- Is the Text field mapped to a NVarChar and not VarCar?
- Is the Text field going to a NOT NULL column?
- Is the Max Length of the Text long enough.
Think about it this way, this class is a parameter to some Entity Framework code. I don't need to test what Entity Framework does with it, because that's Entity Framework's Business, but I do need to test that I am correctly generating the parameter.
If this was code that calculated the users account balance, and that balance was used by EF code to update the database, I wouldn't test the update code, but I sure would test the code that calculated the balance that will be passed to the update code.
This guy asked basically the same question and if you look at his comment on the accepted answer you'll see that he didn't get what he was looking for.
What I'm looking for here is one of the following:
- Here is how you test this:
- A really good reason with an intelligible explanation why I don't need to.