How to unit test referenced property with nhibernate
What's the best practice if I'd like to unit test an entity with a referenced property?
BlogEntry is referencing a User object by a foreign key. Right now I'm using session.Load to avoid an exception since the foreign key cannot contain nulls.
Can i Mock this somehow instead? I don't want my unit test to contain references to a "real" user in the db.
public class BlogEntry {
public virtual int ID {get;set;}
public virtual User CreatedBy {get;set;}
public virtual string Text {get;set;}
}
I'm currently using the following test method:
[Test]
public void Create_blog_entry()
{
using (var session = sessionFactory.OpenSession())
using (var trans = session.BeginTransaction())
{
var entry = new BlogEntry(){
Text = "Lorem ipsum",
CreatedBy = session.Load<User>(1)
};
session.Save(entry);
trans.Rol开发者_StackOverflowlback();
}
}
[Test]
public void Create_blog_entry()
{
using (var session = sessionFactory.OpenSession())
using (var trans = session.BeginTransaction())
{
var user = new User { Id = 1 };
session.Save(user);
var entry = new BlogEntry()
{
Text = "Lorem ipsum",
CreatedBy = user
};
session.Save(entry);
trans.Rollback();
}
}
精彩评论