CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Testing Entity Framework Models (Entities) with Type Mock Live Objects

354 Views
Copy Code Show/Hide Line Numbers
namespace Repository.EntityFramework
{
    //Entity Class Under Test
    public partial class Story
    {
        public ICollection<Tag> Tags
        {
            get
            {
                if(!TagsInternal.IsLoaded)
                {
                    TagsInternal.Load();
                }
                return TagsInternal;
            }
        }
    }
}
 
namespace KiGG.Repository.EntityFramework.Tests
{
    //Test Class
    [Isolated]
    public class StoryFixture
    {
        private readonly Story _story;
 
        public StoryFixture()
        {
            _story = new Story();
        }
 
        [Fact]
        public void Tags_Should_Call_Load_When_IsLoaded_Is_False()
        {
            //Arrange Fake Method Calls on Live Object -None Faked Instance-
            Isolate.WhenCalled(() => _story.TagsInternal.IsLoaded).WillReturn(false);
            Isolate.WhenCalled(() => _story.TagsInternal.Load()).IgnoreCall();
 
            //Act
            ICollection<Tag> tags = _story.Tags;
 
            //Assert
            Isolate.Verify
                   .WasCalledWithAnyArguments(()=>_story.TagsInternal.Load());
 
        }
    }
}
by Muhammad Mosa
  September 01, 2009 @ 2:38pm
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate