Correct syntax for mocking MVVMCross navigation service using Moq











up vote
0
down vote

favorite
1












I am quite new to MVVMCross and Moq and I need some help with the format of mocking the MvxNavigation Service. I have a call in my code which I want to mock.



I would have thought I could have set the return value by doing something along the lines of:



 _naviageService.Setup(n => n.Navigate<PlaceSelectViewModel, Place, Place>(It.IsAny<Place>())).Returns(returnPlace);


but this does not compile. I have tried looking in the Moq quick start and MVVMCross example but can’t seem to find what I want. Please find complete sample below as requested: Tnx



public class FooClass
{
IMvxNavigationService _navigationService;

public IMvxAsyncCommand SelectPlaceCommand { get; }

public FooClass(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
SelectedplaceCommand = new MvxAsyncCommand(SelectPlace);
}

async Task SelectPlace()
{

var place = await _navigationService.Navigate<PlaceSelectViewModel, Place, Place>(new Place());

// Do somehting with place
}

}

[TestFixture]
public class FooTests : MvxIoCSupportingTest
{
Mock<IMvxNavigationService> _navigationService;
FooClass _foo;

[SetUp]
public void SetUp()
{
base.Setup();

_navigationService = new Mock<IMvxNavigationService>();
_foo = new FooClass(_navigationService.Object);
}

[Test]
public async Task DoSomthing_NavigatesToPlaceSelectViewModel()
{
//Arrange


var returnPlace = new Place { MapTitle = "New Place" };

await _navigationService.Setup(n => n.Navigate<PlaceSelectViewModel, Place>(It.IsAny<Place>())).Returns(returnPlace); // ** This is incorrect syntax and does not complile

//Act
await _foo.SelectPlaceCommand.ExecuteAsync();

//Assert
_navigationService.Verify(s => s.Navigate<PlaceSelectViewModel, Place, Place>
(It.IsAny<Place>(),
null,
It.IsAny<CancellationToken>()));
}

}









share|improve this question




















  • 2




    It would be useful if you could provide a Minimal, Complete, and Verifiable example.
    – AndreaT
    Nov 9 at 8:35















up vote
0
down vote

favorite
1












I am quite new to MVVMCross and Moq and I need some help with the format of mocking the MvxNavigation Service. I have a call in my code which I want to mock.



I would have thought I could have set the return value by doing something along the lines of:



 _naviageService.Setup(n => n.Navigate<PlaceSelectViewModel, Place, Place>(It.IsAny<Place>())).Returns(returnPlace);


but this does not compile. I have tried looking in the Moq quick start and MVVMCross example but can’t seem to find what I want. Please find complete sample below as requested: Tnx



public class FooClass
{
IMvxNavigationService _navigationService;

public IMvxAsyncCommand SelectPlaceCommand { get; }

public FooClass(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
SelectedplaceCommand = new MvxAsyncCommand(SelectPlace);
}

async Task SelectPlace()
{

var place = await _navigationService.Navigate<PlaceSelectViewModel, Place, Place>(new Place());

// Do somehting with place
}

}

[TestFixture]
public class FooTests : MvxIoCSupportingTest
{
Mock<IMvxNavigationService> _navigationService;
FooClass _foo;

[SetUp]
public void SetUp()
{
base.Setup();

_navigationService = new Mock<IMvxNavigationService>();
_foo = new FooClass(_navigationService.Object);
}

[Test]
public async Task DoSomthing_NavigatesToPlaceSelectViewModel()
{
//Arrange


var returnPlace = new Place { MapTitle = "New Place" };

await _navigationService.Setup(n => n.Navigate<PlaceSelectViewModel, Place>(It.IsAny<Place>())).Returns(returnPlace); // ** This is incorrect syntax and does not complile

//Act
await _foo.SelectPlaceCommand.ExecuteAsync();

//Assert
_navigationService.Verify(s => s.Navigate<PlaceSelectViewModel, Place, Place>
(It.IsAny<Place>(),
null,
It.IsAny<CancellationToken>()));
}

}









share|improve this question




















  • 2




    It would be useful if you could provide a Minimal, Complete, and Verifiable example.
    – AndreaT
    Nov 9 at 8:35













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I am quite new to MVVMCross and Moq and I need some help with the format of mocking the MvxNavigation Service. I have a call in my code which I want to mock.



I would have thought I could have set the return value by doing something along the lines of:



 _naviageService.Setup(n => n.Navigate<PlaceSelectViewModel, Place, Place>(It.IsAny<Place>())).Returns(returnPlace);


but this does not compile. I have tried looking in the Moq quick start and MVVMCross example but can’t seem to find what I want. Please find complete sample below as requested: Tnx



public class FooClass
{
IMvxNavigationService _navigationService;

public IMvxAsyncCommand SelectPlaceCommand { get; }

public FooClass(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
SelectedplaceCommand = new MvxAsyncCommand(SelectPlace);
}

async Task SelectPlace()
{

var place = await _navigationService.Navigate<PlaceSelectViewModel, Place, Place>(new Place());

// Do somehting with place
}

}

[TestFixture]
public class FooTests : MvxIoCSupportingTest
{
Mock<IMvxNavigationService> _navigationService;
FooClass _foo;

[SetUp]
public void SetUp()
{
base.Setup();

_navigationService = new Mock<IMvxNavigationService>();
_foo = new FooClass(_navigationService.Object);
}

[Test]
public async Task DoSomthing_NavigatesToPlaceSelectViewModel()
{
//Arrange


var returnPlace = new Place { MapTitle = "New Place" };

await _navigationService.Setup(n => n.Navigate<PlaceSelectViewModel, Place>(It.IsAny<Place>())).Returns(returnPlace); // ** This is incorrect syntax and does not complile

//Act
await _foo.SelectPlaceCommand.ExecuteAsync();

//Assert
_navigationService.Verify(s => s.Navigate<PlaceSelectViewModel, Place, Place>
(It.IsAny<Place>(),
null,
It.IsAny<CancellationToken>()));
}

}









share|improve this question















I am quite new to MVVMCross and Moq and I need some help with the format of mocking the MvxNavigation Service. I have a call in my code which I want to mock.



I would have thought I could have set the return value by doing something along the lines of:



 _naviageService.Setup(n => n.Navigate<PlaceSelectViewModel, Place, Place>(It.IsAny<Place>())).Returns(returnPlace);


but this does not compile. I have tried looking in the Moq quick start and MVVMCross example but can’t seem to find what I want. Please find complete sample below as requested: Tnx



public class FooClass
{
IMvxNavigationService _navigationService;

public IMvxAsyncCommand SelectPlaceCommand { get; }

public FooClass(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
SelectedplaceCommand = new MvxAsyncCommand(SelectPlace);
}

async Task SelectPlace()
{

var place = await _navigationService.Navigate<PlaceSelectViewModel, Place, Place>(new Place());

// Do somehting with place
}

}

[TestFixture]
public class FooTests : MvxIoCSupportingTest
{
Mock<IMvxNavigationService> _navigationService;
FooClass _foo;

[SetUp]
public void SetUp()
{
base.Setup();

_navigationService = new Mock<IMvxNavigationService>();
_foo = new FooClass(_navigationService.Object);
}

[Test]
public async Task DoSomthing_NavigatesToPlaceSelectViewModel()
{
//Arrange


var returnPlace = new Place { MapTitle = "New Place" };

await _navigationService.Setup(n => n.Navigate<PlaceSelectViewModel, Place>(It.IsAny<Place>())).Returns(returnPlace); // ** This is incorrect syntax and does not complile

//Act
await _foo.SelectPlaceCommand.ExecuteAsync();

//Assert
_navigationService.Verify(s => s.Navigate<PlaceSelectViewModel, Place, Place>
(It.IsAny<Place>(),
null,
It.IsAny<CancellationToken>()));
}

}






c# xamarin moq mvvmcross






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 13:51

























asked Nov 9 at 8:15









FinniusFrog

255




255








  • 2




    It would be useful if you could provide a Minimal, Complete, and Verifiable example.
    – AndreaT
    Nov 9 at 8:35














  • 2




    It would be useful if you could provide a Minimal, Complete, and Verifiable example.
    – AndreaT
    Nov 9 at 8:35








2




2




It would be useful if you could provide a Minimal, Complete, and Verifiable example.
– AndreaT
Nov 9 at 8:35




It would be useful if you could provide a Minimal, Complete, and Verifiable example.
– AndreaT
Nov 9 at 8:35












1 Answer
1






active

oldest

votes

















up vote
0
down vote













As this issue from the Moq repository explains, you can't just skip optional parameters.



This is kind of a workaround if you are not using all parameters in your Navigate call (or more accurate, if you don't care about them):



_naviageService.Setup(n => n.Navigate<PlaceSelectViewModel, Place, Place>(
It.IsAny<Place>(),
It.IsAny<IMvxBundle>(),
It.IsAny<CancellationToken>())
).Returns(returnPlace);





share|improve this answer





















    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',
    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%2f53222005%2fcorrect-syntax-for-mocking-mvvmcross-navigation-service-using-moq%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








    up vote
    0
    down vote













    As this issue from the Moq repository explains, you can't just skip optional parameters.



    This is kind of a workaround if you are not using all parameters in your Navigate call (or more accurate, if you don't care about them):



    _naviageService.Setup(n => n.Navigate<PlaceSelectViewModel, Place, Place>(
    It.IsAny<Place>(),
    It.IsAny<IMvxBundle>(),
    It.IsAny<CancellationToken>())
    ).Returns(returnPlace);





    share|improve this answer

























      up vote
      0
      down vote













      As this issue from the Moq repository explains, you can't just skip optional parameters.



      This is kind of a workaround if you are not using all parameters in your Navigate call (or more accurate, if you don't care about them):



      _naviageService.Setup(n => n.Navigate<PlaceSelectViewModel, Place, Place>(
      It.IsAny<Place>(),
      It.IsAny<IMvxBundle>(),
      It.IsAny<CancellationToken>())
      ).Returns(returnPlace);





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        As this issue from the Moq repository explains, you can't just skip optional parameters.



        This is kind of a workaround if you are not using all parameters in your Navigate call (or more accurate, if you don't care about them):



        _naviageService.Setup(n => n.Navigate<PlaceSelectViewModel, Place, Place>(
        It.IsAny<Place>(),
        It.IsAny<IMvxBundle>(),
        It.IsAny<CancellationToken>())
        ).Returns(returnPlace);





        share|improve this answer












        As this issue from the Moq repository explains, you can't just skip optional parameters.



        This is kind of a workaround if you are not using all parameters in your Navigate call (or more accurate, if you don't care about them):



        _naviageService.Setup(n => n.Navigate<PlaceSelectViewModel, Place, Place>(
        It.IsAny<Place>(),
        It.IsAny<IMvxBundle>(),
        It.IsAny<CancellationToken>())
        ).Returns(returnPlace);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 at 3:30









        nmilcoff

        873317




        873317






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53222005%2fcorrect-syntax-for-mocking-mvvmcross-navigation-service-using-moq%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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            JBPM : POST request for execute process go wrong