Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8423969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:51:56+00:00 2026-06-10T03:51:56+00:00

I am new to test driven development, and trying to unit test an mvc

  • 0

I am new to test driven development, and trying to unit test an mvc application. I am using Moq and Ninject, and trying to follow the unit of work repository pattern. I am getting a System.ArgumentException Error for all of my tests. Here is the error message and error stack trace:

Test method LOMSv4.Tests.Controllers.AutobytelControllerTest.Index_Contains_All_Requests threw exception: 
System.ArgumentException: Can not instantiate proxy of class: LOMSv4_DAL.Autobytel.Concrete.RequestRepository.
Could not find a parameterless constructor.

Parameter name: constructorArguments

Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(Type proxyType, List`1 proxyArguments, Type classToProxy, Object[] constructorArguments)
Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
Moq.Proxy.CastleProxyFactory.CreateProxy[T](ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
Moq.Mock`1.<InitializeInstance>b__0()
Moq.PexProtector.Invoke(Action action)
Moq.Mock`1.InitializeInstance()
Moq.Mock`1.OnGetObject()
Moq.Mock.GetObject()
Moq.Mock.get_Object()
Moq.MockDefaultValueProvider.ProvideDefault(MethodInfo member)
Moq.QueryableMockExtensions.FluentMock[T,TResult](Mock`1 mock, Expression`1 setup)
lambda_method(Closure )
Moq.Mock.GetInterceptor(Expression fluentExpression, Mock mock)
Moq.Mock.<>c__DisplayClass1c`2.<Setup>b__1b()
Moq.PexProtector.Invoke[T](Func`1 function)
Moq.Mock.Setup[T,TResult](Mock mock, Expression`1 expression, Func`1 condition)
Moq.Mock`1.Setup[TResult](Expression`1 expression)

Here is my test class:

[TestClass]
public class AutobytelControllerTest
{
    Mock<IUnitOfWork> mock = new Mock<IUnitOfWork>();

    [TestMethod]
    public void Index_Contains_All_Requests()
    {
        //Arrange
        AutobytelController controller = new AutobytelController(mock.Object);
        mock.Setup(m => m.RequestRepository.SelectAll()).Returns(new abtRequest[] {
            new abtRequest {RequestID = 1, Title = "Request 1", Description = "Request Description1", UserName = "NewUser", RequestStatus = 0},
            new abtRequest {RequestID = 2, Title = "Request 2", Description = "Request Description2", UserName = "ReturnUser", RequestStatus = 1}
        }.AsQueryable());

        //Act
        abtRequest[] result = ((AutobytelHomeViewModel)controller.Index().Model).Requests.ToArray();
        //Assert
        Assert.AreEqual(result.Length, 2);
        Assert.AreEqual("Request 1", result[0].Title);
        Assert.AreEqual("Request 2", result[1].Title);
    }

I have an IUnitofWork interface and class, generic repository interface and class and a request repository that implements the generic repository

public interface IUnitOfWork
{
    RequestRepository RequestRepository { get; }
    void Save();
}

public class UnitOfWorkRepository : IUnitOfWork, IDisposable
{
    private AutobytelEntities context = new AutobytelEntities();
    private RequestRepository requestRepository;

    public RequestRepository RequestRepository
    {
        get
        {
            if (this.requestRepository == null)
            {
                //this.requestRepository = new GenericRepository<abtRequest>(context);
                this.requestRepository = new RequestRepository(context);
            }
            return requestRepository;
        }
    }

    public void Save()
    {
        context.SaveChanges();
    }

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                context.Dispose();
            }
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
    internal AutobytelEntities context;
    internal IObjectSet<TEntity> objectSet;


    public GenericRepository(AutobytelEntities context)
    {
        this.context = context;
        this.objectSet = context.CreateObjectSet<TEntity>();
    }

    public virtual IQueryable<TEntity> SelectAll()
    {
        return objectSet.AsQueryable();
    }

    public virtual TEntity Update(TEntity entity)
    {
        context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
        return entity;
    }

    public virtual void Delete(TEntity entity)
    {
        objectSet.DeleteObject(entity);
    }
}

    public class RequestRepository : GenericRepository<abtRequest>, IGenericRepository<abtRequest>
{
    public RequestRepository(AutobytelEntities context) : base(context)
    { }

    public virtual abtRequest SelectByUserName(string username)
    {
        return context.abtRequests.FirstOrDefault(i => i.UserName == username && (i.RequestStatus == 0 || i.RequestStatus == 1));
    }

    public virtual abtRequest SelectByRequestID(int requestID)
    {
        return context.abtRequests.FirstOrDefault(i => i.RequestID == requestID);
    }

Using Ninject I am binding my IUnitofwork to UnitOfWork class.

If I add a parameterless constructor to my request repository, My error is resolved- but because I don’t want to create a new object context, I want to pass my context in from the unit of work repository.

How can I resolve this error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:51:57+00:00Added an answer on June 10, 2026 at 3:51 am

    I resolved this error by adding an Interface to my RequestRepository and creating an instance of that interface in my unit of work. My IUnit of Work changed to:

    public interface IUnitOfWork
    {
        IRequestRepository RequestRepository { get; }
        void Save();
    }
    

    Inside my UnitOfWork Repository changed to:

    private IRequestRepository requestRepository;
    
    public IRequestRepository RequestRepository
    {
        get
        {
            if (this.requestRepository == null)
            {
                this.requestRepository = new RequestRepository(context);
            }
            return requestRepository;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to test-driven development (TDD), not yet started using it. But
I am new to grails, coming from Django. Using test driven development, I am
I'm trying to grasp test driven development, and I'm wondering if those unit tests
I am using VS2010 to create a new unit test project to test my
I am trying to become more familiar with test driven development. So far I
I want to create a rails app using Test-Driven Development (TDD), so i wrote
I'm new to test driven development and first time I'm tring to use it
I'm starting a new project and I want to use Test Driven Development, but
I'm wondering what the best practices are in Test Driven Development / testing using
I'm new to Android development as well as test-driven development. I want to write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.