NUnit test of constructor fails because of missing parameter












0















I am trying to run a Unit Test using NUnit and have therefore followed a tutorial, which states the following:



[TestFixture]
public class TestFootballplayerController
{
[Test]
public void FootBallPlayer_CheckingIfControllerReturnsCorrectView_MustReturnTrue()
{
string expected = "CreatePlayer";
FootballplayerController controller = new FootballPlayerController();

var result = controller.CreateIngredient() as ViewResult;

Assert.AreEqual(expected, result.ViewName);
}
}


The tutorial assumes I have a empty default constructor, but my constructor contains a parameter with my DBContext as follows:



    public class FootballplayerController : Controller
{
private readonly FootballContext _context;

public FootballplayerController(FootballContext context)
{
_context = context;
}

public IActionResult CreatePlayer()
{
return View();
}
}


Visual Studio suggests that I create an empty constructor, but in that way I think I will just test something, that shouldn't be tested, instead of the correct constructor with the parameter.



If the answer is obvious, then I must say, I am new to unit testing, and can't work a way around this. Do I have to fake a parameter?










share|improve this question


















  • 2





    you need to inject the context dependency

    – Ehsan Sajjad
    Nov 21 '18 at 16:15











  • As already state you would need to provide an instance of the context (dependency) to the controller being tested.

    – Nkosi
    Nov 21 '18 at 16:25






  • 4





    That said, the controller should be dependent on an abstraction and not a concretion. Other wise in this case you would need to run an integration test with an in-memory database for the context to connect to. If you want to test the controller in isolation then abstract the context out.

    – Nkosi
    Nov 21 '18 at 16:27






  • 1





    Nkosi's comments above are the correct answer to this question

    – Thorin Jacobs
    Nov 21 '18 at 16:30













  • @Nkosi thank you, but does that mean, that I have to use NSubstitute on the controller? The second answer you gave me I don't understand completely, sorry. What confuses me is that I only want to check whether the controller returns the correct view, but feel that it needs a lot more work and knowledge than presumed for a rookie like me.

    – Badabim
    Nov 21 '18 at 19:25
















0















I am trying to run a Unit Test using NUnit and have therefore followed a tutorial, which states the following:



[TestFixture]
public class TestFootballplayerController
{
[Test]
public void FootBallPlayer_CheckingIfControllerReturnsCorrectView_MustReturnTrue()
{
string expected = "CreatePlayer";
FootballplayerController controller = new FootballPlayerController();

var result = controller.CreateIngredient() as ViewResult;

Assert.AreEqual(expected, result.ViewName);
}
}


The tutorial assumes I have a empty default constructor, but my constructor contains a parameter with my DBContext as follows:



    public class FootballplayerController : Controller
{
private readonly FootballContext _context;

public FootballplayerController(FootballContext context)
{
_context = context;
}

public IActionResult CreatePlayer()
{
return View();
}
}


Visual Studio suggests that I create an empty constructor, but in that way I think I will just test something, that shouldn't be tested, instead of the correct constructor with the parameter.



If the answer is obvious, then I must say, I am new to unit testing, and can't work a way around this. Do I have to fake a parameter?










share|improve this question


















  • 2





    you need to inject the context dependency

    – Ehsan Sajjad
    Nov 21 '18 at 16:15











  • As already state you would need to provide an instance of the context (dependency) to the controller being tested.

    – Nkosi
    Nov 21 '18 at 16:25






  • 4





    That said, the controller should be dependent on an abstraction and not a concretion. Other wise in this case you would need to run an integration test with an in-memory database for the context to connect to. If you want to test the controller in isolation then abstract the context out.

    – Nkosi
    Nov 21 '18 at 16:27






  • 1





    Nkosi's comments above are the correct answer to this question

    – Thorin Jacobs
    Nov 21 '18 at 16:30













  • @Nkosi thank you, but does that mean, that I have to use NSubstitute on the controller? The second answer you gave me I don't understand completely, sorry. What confuses me is that I only want to check whether the controller returns the correct view, but feel that it needs a lot more work and knowledge than presumed for a rookie like me.

    – Badabim
    Nov 21 '18 at 19:25














0












0








0


1






I am trying to run a Unit Test using NUnit and have therefore followed a tutorial, which states the following:



[TestFixture]
public class TestFootballplayerController
{
[Test]
public void FootBallPlayer_CheckingIfControllerReturnsCorrectView_MustReturnTrue()
{
string expected = "CreatePlayer";
FootballplayerController controller = new FootballPlayerController();

var result = controller.CreateIngredient() as ViewResult;

Assert.AreEqual(expected, result.ViewName);
}
}


The tutorial assumes I have a empty default constructor, but my constructor contains a parameter with my DBContext as follows:



    public class FootballplayerController : Controller
{
private readonly FootballContext _context;

public FootballplayerController(FootballContext context)
{
_context = context;
}

public IActionResult CreatePlayer()
{
return View();
}
}


Visual Studio suggests that I create an empty constructor, but in that way I think I will just test something, that shouldn't be tested, instead of the correct constructor with the parameter.



If the answer is obvious, then I must say, I am new to unit testing, and can't work a way around this. Do I have to fake a parameter?










share|improve this question














I am trying to run a Unit Test using NUnit and have therefore followed a tutorial, which states the following:



[TestFixture]
public class TestFootballplayerController
{
[Test]
public void FootBallPlayer_CheckingIfControllerReturnsCorrectView_MustReturnTrue()
{
string expected = "CreatePlayer";
FootballplayerController controller = new FootballPlayerController();

var result = controller.CreateIngredient() as ViewResult;

Assert.AreEqual(expected, result.ViewName);
}
}


The tutorial assumes I have a empty default constructor, but my constructor contains a parameter with my DBContext as follows:



    public class FootballplayerController : Controller
{
private readonly FootballContext _context;

public FootballplayerController(FootballContext context)
{
_context = context;
}

public IActionResult CreatePlayer()
{
return View();
}
}


Visual Studio suggests that I create an empty constructor, but in that way I think I will just test something, that shouldn't be tested, instead of the correct constructor with the parameter.



If the answer is obvious, then I must say, I am new to unit testing, and can't work a way around this. Do I have to fake a parameter?







c# asp.net-mvc unit-testing testing nunit






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 16:13









BadabimBadabim

63




63








  • 2





    you need to inject the context dependency

    – Ehsan Sajjad
    Nov 21 '18 at 16:15











  • As already state you would need to provide an instance of the context (dependency) to the controller being tested.

    – Nkosi
    Nov 21 '18 at 16:25






  • 4





    That said, the controller should be dependent on an abstraction and not a concretion. Other wise in this case you would need to run an integration test with an in-memory database for the context to connect to. If you want to test the controller in isolation then abstract the context out.

    – Nkosi
    Nov 21 '18 at 16:27






  • 1





    Nkosi's comments above are the correct answer to this question

    – Thorin Jacobs
    Nov 21 '18 at 16:30













  • @Nkosi thank you, but does that mean, that I have to use NSubstitute on the controller? The second answer you gave me I don't understand completely, sorry. What confuses me is that I only want to check whether the controller returns the correct view, but feel that it needs a lot more work and knowledge than presumed for a rookie like me.

    – Badabim
    Nov 21 '18 at 19:25














  • 2





    you need to inject the context dependency

    – Ehsan Sajjad
    Nov 21 '18 at 16:15











  • As already state you would need to provide an instance of the context (dependency) to the controller being tested.

    – Nkosi
    Nov 21 '18 at 16:25






  • 4





    That said, the controller should be dependent on an abstraction and not a concretion. Other wise in this case you would need to run an integration test with an in-memory database for the context to connect to. If you want to test the controller in isolation then abstract the context out.

    – Nkosi
    Nov 21 '18 at 16:27






  • 1





    Nkosi's comments above are the correct answer to this question

    – Thorin Jacobs
    Nov 21 '18 at 16:30













  • @Nkosi thank you, but does that mean, that I have to use NSubstitute on the controller? The second answer you gave me I don't understand completely, sorry. What confuses me is that I only want to check whether the controller returns the correct view, but feel that it needs a lot more work and knowledge than presumed for a rookie like me.

    – Badabim
    Nov 21 '18 at 19:25








2




2





you need to inject the context dependency

– Ehsan Sajjad
Nov 21 '18 at 16:15





you need to inject the context dependency

– Ehsan Sajjad
Nov 21 '18 at 16:15













As already state you would need to provide an instance of the context (dependency) to the controller being tested.

– Nkosi
Nov 21 '18 at 16:25





As already state you would need to provide an instance of the context (dependency) to the controller being tested.

– Nkosi
Nov 21 '18 at 16:25




4




4





That said, the controller should be dependent on an abstraction and not a concretion. Other wise in this case you would need to run an integration test with an in-memory database for the context to connect to. If you want to test the controller in isolation then abstract the context out.

– Nkosi
Nov 21 '18 at 16:27





That said, the controller should be dependent on an abstraction and not a concretion. Other wise in this case you would need to run an integration test with an in-memory database for the context to connect to. If you want to test the controller in isolation then abstract the context out.

– Nkosi
Nov 21 '18 at 16:27




1




1





Nkosi's comments above are the correct answer to this question

– Thorin Jacobs
Nov 21 '18 at 16:30







Nkosi's comments above are the correct answer to this question

– Thorin Jacobs
Nov 21 '18 at 16:30















@Nkosi thank you, but does that mean, that I have to use NSubstitute on the controller? The second answer you gave me I don't understand completely, sorry. What confuses me is that I only want to check whether the controller returns the correct view, but feel that it needs a lot more work and knowledge than presumed for a rookie like me.

– Badabim
Nov 21 '18 at 19:25





@Nkosi thank you, but does that mean, that I have to use NSubstitute on the controller? The second answer you gave me I don't understand completely, sorry. What confuses me is that I only want to check whether the controller returns the correct view, but feel that it needs a lot more work and knowledge than presumed for a rookie like me.

– Badabim
Nov 21 '18 at 19:25












1 Answer
1






active

oldest

votes


















0














In this case, you can just mock this class using e.g MoQ framework



[Test]
public void FootBallPlayer_CheckingIfControllerReturnsCorrectView_MustReturnTrue()
{
string expected = "CreatePlayer";
var mock = new Mock<FootballContext>();
FootballplayerController controller = new FootballPlayerController(mock.Object);

var result = controller.CreateIngredient() as ViewResult;

Assert.AreEqual(expected, result.ViewName);
}


but you must remember that now is just empty class in this test. You can create better mock, by mocking all your DbSets, and make them IQueryable.






share|improve this answer
























  • that makes sence, but it now gives me the following error "Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: WABI.Models.WABIContext. Could not find a parameterless constructor." I guess it is still looking for that parameterless constructor as in the first place?

    – Badabim
    Nov 23 '18 at 9:02











  • Ended up with creating the empty default constructor, since it didnt effect the unit test. This generates a correct Unit Test, but not the problem, though.

    – Badabim
    Nov 23 '18 at 11:53











  • Do you have parameterless constructor for FootballContext? I remember that i had similar problem with this and Microsoft DI.

    – Wojciech Rak
    Nov 23 '18 at 13:46











  • I ended up creating a parameterless constructor, since finding a way around took too much time and from the comments above it seemed to be a task above my skills. The parameterless constructor solved the problem.

    – Badabim
    Nov 30 '18 at 8:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416218%2fnunit-test-of-constructor-fails-because-of-missing-parameter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














In this case, you can just mock this class using e.g MoQ framework



[Test]
public void FootBallPlayer_CheckingIfControllerReturnsCorrectView_MustReturnTrue()
{
string expected = "CreatePlayer";
var mock = new Mock<FootballContext>();
FootballplayerController controller = new FootballPlayerController(mock.Object);

var result = controller.CreateIngredient() as ViewResult;

Assert.AreEqual(expected, result.ViewName);
}


but you must remember that now is just empty class in this test. You can create better mock, by mocking all your DbSets, and make them IQueryable.






share|improve this answer
























  • that makes sence, but it now gives me the following error "Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: WABI.Models.WABIContext. Could not find a parameterless constructor." I guess it is still looking for that parameterless constructor as in the first place?

    – Badabim
    Nov 23 '18 at 9:02











  • Ended up with creating the empty default constructor, since it didnt effect the unit test. This generates a correct Unit Test, but not the problem, though.

    – Badabim
    Nov 23 '18 at 11:53











  • Do you have parameterless constructor for FootballContext? I remember that i had similar problem with this and Microsoft DI.

    – Wojciech Rak
    Nov 23 '18 at 13:46











  • I ended up creating a parameterless constructor, since finding a way around took too much time and from the comments above it seemed to be a task above my skills. The parameterless constructor solved the problem.

    – Badabim
    Nov 30 '18 at 8:49
















0














In this case, you can just mock this class using e.g MoQ framework



[Test]
public void FootBallPlayer_CheckingIfControllerReturnsCorrectView_MustReturnTrue()
{
string expected = "CreatePlayer";
var mock = new Mock<FootballContext>();
FootballplayerController controller = new FootballPlayerController(mock.Object);

var result = controller.CreateIngredient() as ViewResult;

Assert.AreEqual(expected, result.ViewName);
}


but you must remember that now is just empty class in this test. You can create better mock, by mocking all your DbSets, and make them IQueryable.






share|improve this answer
























  • that makes sence, but it now gives me the following error "Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: WABI.Models.WABIContext. Could not find a parameterless constructor." I guess it is still looking for that parameterless constructor as in the first place?

    – Badabim
    Nov 23 '18 at 9:02











  • Ended up with creating the empty default constructor, since it didnt effect the unit test. This generates a correct Unit Test, but not the problem, though.

    – Badabim
    Nov 23 '18 at 11:53











  • Do you have parameterless constructor for FootballContext? I remember that i had similar problem with this and Microsoft DI.

    – Wojciech Rak
    Nov 23 '18 at 13:46











  • I ended up creating a parameterless constructor, since finding a way around took too much time and from the comments above it seemed to be a task above my skills. The parameterless constructor solved the problem.

    – Badabim
    Nov 30 '18 at 8:49














0












0








0







In this case, you can just mock this class using e.g MoQ framework



[Test]
public void FootBallPlayer_CheckingIfControllerReturnsCorrectView_MustReturnTrue()
{
string expected = "CreatePlayer";
var mock = new Mock<FootballContext>();
FootballplayerController controller = new FootballPlayerController(mock.Object);

var result = controller.CreateIngredient() as ViewResult;

Assert.AreEqual(expected, result.ViewName);
}


but you must remember that now is just empty class in this test. You can create better mock, by mocking all your DbSets, and make them IQueryable.






share|improve this answer













In this case, you can just mock this class using e.g MoQ framework



[Test]
public void FootBallPlayer_CheckingIfControllerReturnsCorrectView_MustReturnTrue()
{
string expected = "CreatePlayer";
var mock = new Mock<FootballContext>();
FootballplayerController controller = new FootballPlayerController(mock.Object);

var result = controller.CreateIngredient() as ViewResult;

Assert.AreEqual(expected, result.ViewName);
}


but you must remember that now is just empty class in this test. You can create better mock, by mocking all your DbSets, and make them IQueryable.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 22:46









Wojciech RakWojciech Rak

34




34













  • that makes sence, but it now gives me the following error "Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: WABI.Models.WABIContext. Could not find a parameterless constructor." I guess it is still looking for that parameterless constructor as in the first place?

    – Badabim
    Nov 23 '18 at 9:02











  • Ended up with creating the empty default constructor, since it didnt effect the unit test. This generates a correct Unit Test, but not the problem, though.

    – Badabim
    Nov 23 '18 at 11:53











  • Do you have parameterless constructor for FootballContext? I remember that i had similar problem with this and Microsoft DI.

    – Wojciech Rak
    Nov 23 '18 at 13:46











  • I ended up creating a parameterless constructor, since finding a way around took too much time and from the comments above it seemed to be a task above my skills. The parameterless constructor solved the problem.

    – Badabim
    Nov 30 '18 at 8:49



















  • that makes sence, but it now gives me the following error "Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: WABI.Models.WABIContext. Could not find a parameterless constructor." I guess it is still looking for that parameterless constructor as in the first place?

    – Badabim
    Nov 23 '18 at 9:02











  • Ended up with creating the empty default constructor, since it didnt effect the unit test. This generates a correct Unit Test, but not the problem, though.

    – Badabim
    Nov 23 '18 at 11:53











  • Do you have parameterless constructor for FootballContext? I remember that i had similar problem with this and Microsoft DI.

    – Wojciech Rak
    Nov 23 '18 at 13:46











  • I ended up creating a parameterless constructor, since finding a way around took too much time and from the comments above it seemed to be a task above my skills. The parameterless constructor solved the problem.

    – Badabim
    Nov 30 '18 at 8:49

















that makes sence, but it now gives me the following error "Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: WABI.Models.WABIContext. Could not find a parameterless constructor." I guess it is still looking for that parameterless constructor as in the first place?

– Badabim
Nov 23 '18 at 9:02





that makes sence, but it now gives me the following error "Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: WABI.Models.WABIContext. Could not find a parameterless constructor." I guess it is still looking for that parameterless constructor as in the first place?

– Badabim
Nov 23 '18 at 9:02













Ended up with creating the empty default constructor, since it didnt effect the unit test. This generates a correct Unit Test, but not the problem, though.

– Badabim
Nov 23 '18 at 11:53





Ended up with creating the empty default constructor, since it didnt effect the unit test. This generates a correct Unit Test, but not the problem, though.

– Badabim
Nov 23 '18 at 11:53













Do you have parameterless constructor for FootballContext? I remember that i had similar problem with this and Microsoft DI.

– Wojciech Rak
Nov 23 '18 at 13:46





Do you have parameterless constructor for FootballContext? I remember that i had similar problem with this and Microsoft DI.

– Wojciech Rak
Nov 23 '18 at 13:46













I ended up creating a parameterless constructor, since finding a way around took too much time and from the comments above it seemed to be a task above my skills. The parameterless constructor solved the problem.

– Badabim
Nov 30 '18 at 8:49





I ended up creating a parameterless constructor, since finding a way around took too much time and from the comments above it seemed to be a task above my skills. The parameterless constructor solved the problem.

– Badabim
Nov 30 '18 at 8:49


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416218%2fnunit-test-of-constructor-fails-because-of-missing-parameter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin