Correct syntax for mocking MVVMCross navigation service using Moq
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
2
It would be useful if you could provide a Minimal, Complete, and Verifiable example.
– AndreaT
Nov 9 at 8:35
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
c# xamarin moq mvvmcross
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
add a comment |
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
add a comment |
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);
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 27 at 3:30
nmilcoff
873317
873317
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
It would be useful if you could provide a Minimal, Complete, and Verifiable example.
– AndreaT
Nov 9 at 8:35