Mocking Azure MobileServiceClient
up vote
1
down vote
favorite
I have a method I need to Unittest and I am stuck with mocking part of it.
private async void LoginCommandAsync()
.....
CancellationToken ct;
var result = await ClientAuthorizationService.CurrentClient.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct);
....
I tried to mock the Microsoft.WindowsAzure.MobileServices.MobileServiceClient using Moq. Unfortunatly I always get a System.InvalidCastException in the last code line
var currentClient = new Mock<IMobileServiceClient>(MockBehavior.Loose);
currentClient.Setup(x => x.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct)).Returns(Task.FromResult(token));
var disposableCurrentClient = currentClient.As<IDisposable>();
ClientAuthorizationService.CurrentClient = (MobileServiceClient)disposableCurrentClient.Object;
MobileServiceClient inherits from IMobileServiceClient, IDisposable but no matter how I try to use/assign the mocked object... it does not work.
public class ClientAuthorizationService : IClientAuthorizationService
public MobileServiceClient CurrentClient get; set;
public ClientAuthorizationService()
CurrentClient = new MobileServiceClient(Constants.ApiConstants.ApplicationUrl);
c# unit-testing moq azure-mobile-services
add a comment |
up vote
1
down vote
favorite
I have a method I need to Unittest and I am stuck with mocking part of it.
private async void LoginCommandAsync()
.....
CancellationToken ct;
var result = await ClientAuthorizationService.CurrentClient.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct);
....
I tried to mock the Microsoft.WindowsAzure.MobileServices.MobileServiceClient using Moq. Unfortunatly I always get a System.InvalidCastException in the last code line
var currentClient = new Mock<IMobileServiceClient>(MockBehavior.Loose);
currentClient.Setup(x => x.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct)).Returns(Task.FromResult(token));
var disposableCurrentClient = currentClient.As<IDisposable>();
ClientAuthorizationService.CurrentClient = (MobileServiceClient)disposableCurrentClient.Object;
MobileServiceClient inherits from IMobileServiceClient, IDisposable but no matter how I try to use/assign the mocked object... it does not work.
public class ClientAuthorizationService : IClientAuthorizationService
public MobileServiceClient CurrentClient get; set;
public ClientAuthorizationService()
CurrentClient = new MobileServiceClient(Constants.ApiConstants.ApplicationUrl);
c# unit-testing moq azure-mobile-services
You are mockingIMobileServiceClient
at no point is that interface going to be aMobileServiceClient
class. This feels like an XY problem. What are you actually trying to do?
– Nkosi
Nov 11 at 20:52
What isClientAuthorizationService
andClientAuthorizationService.CurrentClient
?
– Nkosi
Nov 11 at 20:58
I am trying to Mock the MobileServiceClient because of the InvokeApiAsync that occurs in a method that I need to unittest. Currently the CurrentClient of type MobileServiceClient is a member/property of the ClientAuthorizationService which is an own implementation of the IClientAuthorizationService with no other dependencies
– Oliver Martins
Nov 11 at 22:58
edit the question with a Minimal, Complete, and Verifiable example the properly represents the problem to be solved so we can better help you.
– Nkosi
Nov 11 at 22:59
as suggested, I edited my original question. Let me know if you need more information
– Oliver Martins
Nov 11 at 23:31
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a method I need to Unittest and I am stuck with mocking part of it.
private async void LoginCommandAsync()
.....
CancellationToken ct;
var result = await ClientAuthorizationService.CurrentClient.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct);
....
I tried to mock the Microsoft.WindowsAzure.MobileServices.MobileServiceClient using Moq. Unfortunatly I always get a System.InvalidCastException in the last code line
var currentClient = new Mock<IMobileServiceClient>(MockBehavior.Loose);
currentClient.Setup(x => x.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct)).Returns(Task.FromResult(token));
var disposableCurrentClient = currentClient.As<IDisposable>();
ClientAuthorizationService.CurrentClient = (MobileServiceClient)disposableCurrentClient.Object;
MobileServiceClient inherits from IMobileServiceClient, IDisposable but no matter how I try to use/assign the mocked object... it does not work.
public class ClientAuthorizationService : IClientAuthorizationService
public MobileServiceClient CurrentClient get; set;
public ClientAuthorizationService()
CurrentClient = new MobileServiceClient(Constants.ApiConstants.ApplicationUrl);
c# unit-testing moq azure-mobile-services
I have a method I need to Unittest and I am stuck with mocking part of it.
private async void LoginCommandAsync()
.....
CancellationToken ct;
var result = await ClientAuthorizationService.CurrentClient.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct);
....
I tried to mock the Microsoft.WindowsAzure.MobileServices.MobileServiceClient using Moq. Unfortunatly I always get a System.InvalidCastException in the last code line
var currentClient = new Mock<IMobileServiceClient>(MockBehavior.Loose);
currentClient.Setup(x => x.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct)).Returns(Task.FromResult(token));
var disposableCurrentClient = currentClient.As<IDisposable>();
ClientAuthorizationService.CurrentClient = (MobileServiceClient)disposableCurrentClient.Object;
MobileServiceClient inherits from IMobileServiceClient, IDisposable but no matter how I try to use/assign the mocked object... it does not work.
public class ClientAuthorizationService : IClientAuthorizationService
public MobileServiceClient CurrentClient get; set;
public ClientAuthorizationService()
CurrentClient = new MobileServiceClient(Constants.ApiConstants.ApplicationUrl);
c# unit-testing moq azure-mobile-services
c# unit-testing moq azure-mobile-services
edited Nov 11 at 23:48
Nkosi
108k16113182
108k16113182
asked Nov 11 at 18:45
Oliver Martins
154
154
You are mockingIMobileServiceClient
at no point is that interface going to be aMobileServiceClient
class. This feels like an XY problem. What are you actually trying to do?
– Nkosi
Nov 11 at 20:52
What isClientAuthorizationService
andClientAuthorizationService.CurrentClient
?
– Nkosi
Nov 11 at 20:58
I am trying to Mock the MobileServiceClient because of the InvokeApiAsync that occurs in a method that I need to unittest. Currently the CurrentClient of type MobileServiceClient is a member/property of the ClientAuthorizationService which is an own implementation of the IClientAuthorizationService with no other dependencies
– Oliver Martins
Nov 11 at 22:58
edit the question with a Minimal, Complete, and Verifiable example the properly represents the problem to be solved so we can better help you.
– Nkosi
Nov 11 at 22:59
as suggested, I edited my original question. Let me know if you need more information
– Oliver Martins
Nov 11 at 23:31
add a comment |
You are mockingIMobileServiceClient
at no point is that interface going to be aMobileServiceClient
class. This feels like an XY problem. What are you actually trying to do?
– Nkosi
Nov 11 at 20:52
What isClientAuthorizationService
andClientAuthorizationService.CurrentClient
?
– Nkosi
Nov 11 at 20:58
I am trying to Mock the MobileServiceClient because of the InvokeApiAsync that occurs in a method that I need to unittest. Currently the CurrentClient of type MobileServiceClient is a member/property of the ClientAuthorizationService which is an own implementation of the IClientAuthorizationService with no other dependencies
– Oliver Martins
Nov 11 at 22:58
edit the question with a Minimal, Complete, and Verifiable example the properly represents the problem to be solved so we can better help you.
– Nkosi
Nov 11 at 22:59
as suggested, I edited my original question. Let me know if you need more information
– Oliver Martins
Nov 11 at 23:31
You are mocking
IMobileServiceClient
at no point is that interface going to be a MobileServiceClient
class. This feels like an XY problem. What are you actually trying to do?– Nkosi
Nov 11 at 20:52
You are mocking
IMobileServiceClient
at no point is that interface going to be a MobileServiceClient
class. This feels like an XY problem. What are you actually trying to do?– Nkosi
Nov 11 at 20:52
What is
ClientAuthorizationService
and ClientAuthorizationService.CurrentClient
?– Nkosi
Nov 11 at 20:58
What is
ClientAuthorizationService
and ClientAuthorizationService.CurrentClient
?– Nkosi
Nov 11 at 20:58
I am trying to Mock the MobileServiceClient because of the InvokeApiAsync that occurs in a method that I need to unittest. Currently the CurrentClient of type MobileServiceClient is a member/property of the ClientAuthorizationService which is an own implementation of the IClientAuthorizationService with no other dependencies
– Oliver Martins
Nov 11 at 22:58
I am trying to Mock the MobileServiceClient because of the InvokeApiAsync that occurs in a method that I need to unittest. Currently the CurrentClient of type MobileServiceClient is a member/property of the ClientAuthorizationService which is an own implementation of the IClientAuthorizationService with no other dependencies
– Oliver Martins
Nov 11 at 22:58
edit the question with a Minimal, Complete, and Verifiable example the properly represents the problem to be solved so we can better help you.
– Nkosi
Nov 11 at 22:59
edit the question with a Minimal, Complete, and Verifiable example the properly represents the problem to be solved so we can better help you.
– Nkosi
Nov 11 at 22:59
as suggested, I edited my original question. Let me know if you need more information
– Oliver Martins
Nov 11 at 23:31
as suggested, I edited my original question. Let me know if you need more information
– Oliver Martins
Nov 11 at 23:31
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Change ClientAuthorizationService.CurrentClient
member/property from MobileServiceClient
to IMobileServiceClient
public interface IClientAuthorizationService
IMobileServiceClient CurrentClient get; set;
public class ClientAuthorizationService : IClientAuthorizationService
public ClientAuthorizationService()
CurrentClient = new MobileServiceClient(Constants.ApiConstants.ApplicationUrl);
public IMobileServiceClient CurrentClient get; set;
This will allow for a mock of the client to be assigned when testing as ClientAuthorizationService
is now dependent on abstractions and not implementation concerns.
//....
var clientAuthorizationService = new Mock<IClientAuthorizationService>();
var currentClient = new Mock<IMobileServiceClient>();
currentClient
.Setup(_ => _.InvokeApiAsync(It.IsAny<string>(), null, HttpMethod.Get, null, It.IsAny<CancellationToken>()))
.ReturnsAsync(token);
clientAuthorizationService
.Setup(_ => _.CurrentClient)
.Returns(currentClient.Object);
//.....
Also it is advised not to use async void
except for event handlers
So the code under test needs to be updated accordingly.
private async Task LoginCommandAsync()
//.....
CancellationToken ct;
var result = await ClientAuthorizationService.CurrentClient.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct);
//....
add a comment |
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
);
);
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%2f53251995%2fmocking-azure-mobileserviceclient%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
accepted
Change ClientAuthorizationService.CurrentClient
member/property from MobileServiceClient
to IMobileServiceClient
public interface IClientAuthorizationService
IMobileServiceClient CurrentClient get; set;
public class ClientAuthorizationService : IClientAuthorizationService
public ClientAuthorizationService()
CurrentClient = new MobileServiceClient(Constants.ApiConstants.ApplicationUrl);
public IMobileServiceClient CurrentClient get; set;
This will allow for a mock of the client to be assigned when testing as ClientAuthorizationService
is now dependent on abstractions and not implementation concerns.
//....
var clientAuthorizationService = new Mock<IClientAuthorizationService>();
var currentClient = new Mock<IMobileServiceClient>();
currentClient
.Setup(_ => _.InvokeApiAsync(It.IsAny<string>(), null, HttpMethod.Get, null, It.IsAny<CancellationToken>()))
.ReturnsAsync(token);
clientAuthorizationService
.Setup(_ => _.CurrentClient)
.Returns(currentClient.Object);
//.....
Also it is advised not to use async void
except for event handlers
So the code under test needs to be updated accordingly.
private async Task LoginCommandAsync()
//.....
CancellationToken ct;
var result = await ClientAuthorizationService.CurrentClient.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct);
//....
add a comment |
up vote
0
down vote
accepted
Change ClientAuthorizationService.CurrentClient
member/property from MobileServiceClient
to IMobileServiceClient
public interface IClientAuthorizationService
IMobileServiceClient CurrentClient get; set;
public class ClientAuthorizationService : IClientAuthorizationService
public ClientAuthorizationService()
CurrentClient = new MobileServiceClient(Constants.ApiConstants.ApplicationUrl);
public IMobileServiceClient CurrentClient get; set;
This will allow for a mock of the client to be assigned when testing as ClientAuthorizationService
is now dependent on abstractions and not implementation concerns.
//....
var clientAuthorizationService = new Mock<IClientAuthorizationService>();
var currentClient = new Mock<IMobileServiceClient>();
currentClient
.Setup(_ => _.InvokeApiAsync(It.IsAny<string>(), null, HttpMethod.Get, null, It.IsAny<CancellationToken>()))
.ReturnsAsync(token);
clientAuthorizationService
.Setup(_ => _.CurrentClient)
.Returns(currentClient.Object);
//.....
Also it is advised not to use async void
except for event handlers
So the code under test needs to be updated accordingly.
private async Task LoginCommandAsync()
//.....
CancellationToken ct;
var result = await ClientAuthorizationService.CurrentClient.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct);
//....
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Change ClientAuthorizationService.CurrentClient
member/property from MobileServiceClient
to IMobileServiceClient
public interface IClientAuthorizationService
IMobileServiceClient CurrentClient get; set;
public class ClientAuthorizationService : IClientAuthorizationService
public ClientAuthorizationService()
CurrentClient = new MobileServiceClient(Constants.ApiConstants.ApplicationUrl);
public IMobileServiceClient CurrentClient get; set;
This will allow for a mock of the client to be assigned when testing as ClientAuthorizationService
is now dependent on abstractions and not implementation concerns.
//....
var clientAuthorizationService = new Mock<IClientAuthorizationService>();
var currentClient = new Mock<IMobileServiceClient>();
currentClient
.Setup(_ => _.InvokeApiAsync(It.IsAny<string>(), null, HttpMethod.Get, null, It.IsAny<CancellationToken>()))
.ReturnsAsync(token);
clientAuthorizationService
.Setup(_ => _.CurrentClient)
.Returns(currentClient.Object);
//.....
Also it is advised not to use async void
except for event handlers
So the code under test needs to be updated accordingly.
private async Task LoginCommandAsync()
//.....
CancellationToken ct;
var result = await ClientAuthorizationService.CurrentClient.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct);
//....
Change ClientAuthorizationService.CurrentClient
member/property from MobileServiceClient
to IMobileServiceClient
public interface IClientAuthorizationService
IMobileServiceClient CurrentClient get; set;
public class ClientAuthorizationService : IClientAuthorizationService
public ClientAuthorizationService()
CurrentClient = new MobileServiceClient(Constants.ApiConstants.ApplicationUrl);
public IMobileServiceClient CurrentClient get; set;
This will allow for a mock of the client to be assigned when testing as ClientAuthorizationService
is now dependent on abstractions and not implementation concerns.
//....
var clientAuthorizationService = new Mock<IClientAuthorizationService>();
var currentClient = new Mock<IMobileServiceClient>();
currentClient
.Setup(_ => _.InvokeApiAsync(It.IsAny<string>(), null, HttpMethod.Get, null, It.IsAny<CancellationToken>()))
.ReturnsAsync(token);
clientAuthorizationService
.Setup(_ => _.CurrentClient)
.Returns(currentClient.Object);
//.....
Also it is advised not to use async void
except for event handlers
So the code under test needs to be updated accordingly.
private async Task LoginCommandAsync()
//.....
CancellationToken ct;
var result = await ClientAuthorizationService.CurrentClient.InvokeApiAsync(Constants.ApiConstants.AzureAuthMeData,
null, HttpMethod.Get, null, ct);
//....
edited Nov 11 at 23:55
answered Nov 11 at 23:47
Nkosi
108k16113182
108k16113182
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%2f53251995%2fmocking-azure-mobileserviceclient%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
You are mocking
IMobileServiceClient
at no point is that interface going to be aMobileServiceClient
class. This feels like an XY problem. What are you actually trying to do?– Nkosi
Nov 11 at 20:52
What is
ClientAuthorizationService
andClientAuthorizationService.CurrentClient
?– Nkosi
Nov 11 at 20:58
I am trying to Mock the MobileServiceClient because of the InvokeApiAsync that occurs in a method that I need to unittest. Currently the CurrentClient of type MobileServiceClient is a member/property of the ClientAuthorizationService which is an own implementation of the IClientAuthorizationService with no other dependencies
– Oliver Martins
Nov 11 at 22:58
edit the question with a Minimal, Complete, and Verifiable example the properly represents the problem to be solved so we can better help you.
– Nkosi
Nov 11 at 22:59
as suggested, I edited my original question. Let me know if you need more information
– Oliver Martins
Nov 11 at 23:31