Note that there are some explanatory texts on larger screens.

plurals
  1. PORhino Mocks - Testing Repository layer returns "object reference not set to instance" error
    primarykey
    data
    text
    <p>Its prudent that I firstly say I'm new to both Rhino Mocks and to mocking more generally.</p> <p>With that in mind i'm attempting to Unit test my Linq to SQL repository layer to ensure that the correct methods on the datacontext are being hit, and that the LINQ to SQL is filtering correctly.</p> <p>~EDITED for clarity~</p> <p>The method in question - 'GetRecordWhere' - is defined in the Repository class. It calls the method - 'GetTable' - on the DataContextWrapper which is my custom wrapper around the Linq to SQL DataContext (auto-generated) which was implemented to make the DataContext mockable.</p> <pre><code>public interface IDataContextWrapper : IDisposable { IQueryable&lt;TName&gt; GetTable&lt;TName&gt;() where TName : class; } public class DataContextWrapper : IDataContextWrapper { public IQueryable&lt;TName&gt; GetTable&lt;TName&gt;() where TName : class { return _db.GetTable&lt;TName&gt;().AsQueryable(); } } public class Repository : IRepository { public T GetRecordWhere&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; predicate) where T : class { return _dataContext.GetTable&lt;T&gt;().Where(predicate).SingleOrDefault(); } } </code></pre> <p>The error I am currently faced with is thrown when attempting to stub the 'GetTable' method to provide a queryable result set which can be queryed using the 'GetRecordWhere' method.</p> <p>The ArgumentNullExcpetion: value cannot be null. is thrown with reference to the line:</p> <pre><code>Arg&lt;Expression&lt;Func&lt;Billing, bool&gt;&gt;&gt;.Is.Anything </code></pre> <p>.. which I have also tried with Is.NotNull and a specific predicate.</p> <p>Unit test example:</p> <pre><code> _dataContext = MockRepository.GenerateMock&lt;IDataContextWrapper&gt;(); [Test] public void GetRecordWhere() { // Arrange var billing = new Billing { BillingId = 1 }; var billingQueryList = new List&lt;Billing&gt; {billing}; const int testId = 1; _dataContext.Stub(x =&gt; x.GetTable&lt;Billing&gt;() .Where(Arg&lt;Expression&lt;Func&lt;Billing, bool&gt;&gt;&gt;.Is.Anything) .SingleOrDefault()) .Return(billing); _intRepository = new Repository(_dataContext); // Act var result = _intRepository.GetRecordWhere&lt;Billing&gt;(x =&gt; x.BillingId == testId); // Assert Assert.IsNotNull(result); Assert.AreEqual(result.BillingId, testId); _dataContext.AssertWasCalled(x =&gt; x.GetTable&lt;Billing&gt;()); } </code></pre> <p>Is this a failing in my understanding of RhinoMocks? </p> <p>Help is appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload