Hi,

i found 
this: 
http://pipersgates.wordpress.com/2014/03/25/mocking-entity-framework-6-context-with-rhino-mocks-code-first/
and of course what Microsoft 
stated: http://msdn.microsoft.com/en-us/data/dn314429.aspx

But it fails when I am trying to adapt it.

[TestClass]
public class MyTestClass
{    
      [TestMethod]
      public void MyTestMethod(){
 
            var mockRepository = new MockRepository();

            var list = new List<Employee>
            {
                new Employee{ Id = 1},
                new Employee{ Id = 2},
                new Employee{ Id = 3}

            }.AsQueryable();

            var mockEntities = MockRepository.GenerateMock<Entities>();

            IDbSet<Employee> mockDbSet= 
MockRepository.GenerateMock<IDbSet<Employee>, IQueryable>();


            mockDbSet.AsQueryable().Stub(m => 
m.Provider).Return(list .Provider);
            mockDbSet.AsQueryable().Stub(m => 
m.Expression).Return(list .Expression);
            mockDbSet.AsQueryable().Stub(m => 
m.ElementType).Return(list .ElementType);
            mockDbSet.AsQueryable().Stub(m => 
m.GetEnumerator()).Return(list .GetEnumerator());

            mockEntities.Stub(x => x.Employees).PropertyBehavior();

            mockEntities.Employees = mockDbSet as DbSet<Employee>;



            int expected = list .Count();

            var employeesService= mockRepository.PartialMock<Employees
Service>();

            int actual = employeesService.GetCount();

            Assert.AreEqual(expected, actual);

            mockRepository.VerifyAll();
       }
}

public partial class Entities : DbContext
{
        public Entities()
            : base("name=Entities")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public virtual DbSet<Employee> Employees{ get; set; }
}


public class EmployeesService
{
     Entities context;

     public EmployeeService(Entities context)
     {
        this.context=context;
     }

           public int GetCount()
           {                   
                  return context.Employees.Count();                    
           }
}

mockDbSet is null after save casting to DbSet<Employee>. Therefor 
GetCount() throws a nullpointerreference because the property Employees is 
not set. 

Does anyone know what I am doing wrong? 

It is just a sample, no guarantee the code works as stated. ;) it just 
visualize things. 


Thanks!!

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rhinomocks.
For more options, visit https://groups.google.com/d/optout.

Reply via email to