Can anybody help me out with an example on how to fake below assembly reference
System.ServiceModel.Channels.CustomBinding?
When we generate the fakes assembly there are only stubs in that namespace no shims.
I am trying to fake the below initialization but getting an error when trying to stub the custom and NetTcpBinding binding.
Dim lobjBinding1 As CustomBinding = Nothing
lobjBinding1 = New CustomBinding(New NetTcpBinding(lstrRawBindingName))
Related
Hi I am new to unit testing and I am trying to mock the 'annotationManager' of the PDFtron webviewer
I am using this below code in my test file
jest.mock('#pdftron/webviewer', () =>({
annotationManager: {
getAnnotationsList: jest.fn().mockReturnValue([]),
deleteAnnotations: jest.fn()
},
}));
In the code, I'm getting the list of annotations using 'getAnnotationsList' function and deleting it using 'deleteAnnotations' function.
In the log of the unit tests, I'm getting this following error
'cannot read the properties of undefined (reading 'getAnnotationsList')
Is this the correct way to doing things or am I missing something?
are you able to share an example of a test you are writing where you need to mock the annotation manager? Depending on how you are using the WebViewer package, mocking the annotation manager can be different. If you prefer to reach out to Apryse directly you can also reach out to them via their support form
Can anyone please help me understand the below piece of code? What does the second line doing? I am unable to break down that code. I am learning how to write webservices using JAX-RS and happened to come by this code.
String responsedata =
"{\"custId\":"+list.get(0)+",\"accountId\":"list.get(1)+"}";
return Response.status(201).entity(responsedata).build();
You're creating a Response object using fluent interface. Response.status() sets the status code and returns a Response.ResponseBuilder and sets the response entity body Response.entity() and also returns the same builder. Whenever you have a builder, you are using the Builder pattern. When you're done building the object, you call build() which will return the final object. In this case, it returns the Response object.
I am new to writing test cases for WebAPI's. I have seen similar questions asked in the past, but not answered, but I am wondering how I would test my APIs if they have an ODataQueryOptions as part of the parameters. See below:
public IQueryable<Item> GetByIdAndLocale(ODataQueryOptions opts,
Guid actionuniqueid,
string actionsecondaryid)
Would I have to moq this? If so, how would this look? Any help would be appreciated.
For ODataQueryOptions perspective, you may want to test that all the OData query options can work with your Function. So firstly you need to create an instance of ODataQueryOptions. Here is an example:
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
ODataQueryContext context = new ODataQueryContext(EdmCoreModel.Instance, elementType);
ODataQueryOptions options = new ODataQueryOptions(context, request);
So you need to create your own EDM model to replace EdmCoreModel.Instance, and replace requestUri with your query. elemntType in ODataQueryContext is "The CLR type of the element of the collection being queried".
I cannot tell from the phrasing, but is the above call (GetByIdAndLocale) the Web API that you are trying to test or are you trying to test something that is calling it?
One uses a mock or a stub to replace dependencies in a Unit Under Test (UUT). If you are testing GetByIdAndLocale() then you would not mock it though if it calls something else that takes the ODataQueryOptions as a parameter, you could use Moq to stub/mock that call.
If you are testing some unit that calls GetByIdAndLocale() then, yes, you could moq it. How exactly you might do this depends upon the goal (checking that the correct values are being passed in vs. checking that the returned IQueryable is correctly processed) basically, matching against It.IsAny() or against some matcher.
Which do you want to test?
GetByIdAndLocale(), something that calls it or something (not shown) that it calls?
What are you interested in verifying?
Correct options are passed in or the processing of the return from the mocked call?
I have a Visual Studio 2012 Project and the following NuGet Packages installed:
AutoFixture with Auto Mocking using Moq
Autofixture with xUnit.net data theories
AutoFixture
Moq
xUnit.net: Extensions
xUnit.net: Runners
xUnit.net
Given the following contrived Logger class (Logger.fs):
namespace FUnit
type public ILoggerContext =
abstract member LogPath :string
type public LoggerContext (logPath :string) =
member val LogPath = logPath with get, set
interface ILoggerContext with
member this.LogPath = this.LogPath
type public Logger () =
member this.Log (context: ILoggerContext) value =
System.String.Format("Logged {0} to {1}.", value, context.LogPath)
and the following unit test:
namespace FUnit.Test
open FUnit
type public Math_Add() =
[<Xunit.Extensions.Theory>]
[<Ploeh.AutoFixture.Xunit.AutoData>]
member this.``Should Output Value And Path`` (path :string) =
let context = new LoggerContext(path)
let logger = new Logger()
let expected = System.String.Format("Logged value to {0}.", path)
let actual = logger.Log context "value"
Xunit.Assert.Equal<string>(expected, actual)
The Test Explorer does not recognize the unit test after ensuring I'm showing all tests and building the project. The project builds correctly with no errors or warnings in the Build, General, or Test output logs.
If I replace the current Theory and AutoData attributes with an Fact attribute, the test shows up.
Is AutoFixture supported in F# test projects?
Can anyone else replicate this and know what I'm doing wrong?
I think this is an issue with conflicting versions of Xunit.Extensions used by Ploeh.Autofixture.Xunit and the test runner.
If you run the tests using Xunit's runners then you should see an exception complaining about not being able to find a specific version of Xunit.Extensions.
You can fix this by adding binding redirects to your test projects app.config. Since you're using NuGet you can have it generate the binding redirects by running this in the Package Manager Console for your test project.
Add-BindingRedirect
You can then tweak the generated binding redirects in app.config to be more specific if you want.
Once you rebuild the project you should see the test appear in VS Test Explorer.
I'm implementing a client consuming a webservice. I want to reduce dependencies and decided to mock the webservice.
I use mockito, it has the advantage vs. EasyMock to be able to mock classes, not just interfaces. But that's not the point.
In my test, I've got this code:
// Mock the required objects
Document mDocument = mock(Document.class);
Element mRootElement = mock(Element.class);
Element mGeonameElement = mock(Element.class);
Element mLatElement = mock(Element.class);
Element mLonElement = mock(Element.class);
// record their behavior
when(mDocument.getRootElement()).thenReturn(mRootElement);
when(mRootElement.getChild("geoname")).thenReturn(mGeonameElement);
when(mGeonameElement.getChild("lat")).thenReturn(mLatElement);
when(mGeonameElement.getChild("lon")).thenReturn(mLonElement);
// A_LOCATION_BEAN is a simple pojo for lat & lon, don't care about it!
when(mLatElement.getText()).thenReturn(
Float.toString(A_LOCATION_BEAN.getLat()));
when(mLonElement.getText()).thenReturn(
Float.toString(A_LOCATION_BEAN.getLon()));
// let it work!
GeoLocationFetcher geoLocationFetcher = GeoLocationFetcher
.getInstance();
LocationBean locationBean = geoLocationFetcher
.extractGeoLocationFromXml(mDocument);
// verify their behavior
verify(mDocument).getRootElement();
verify(mRootElement).getChild("geoname");
verify(mGeonameElement).getChild("lat");
verify(mGeonameElement).getChild("lon");
verify(mLatElement).getText();
verify(mLonElement).getText();
assertEquals(A_LOCATION_BEAN, locationBean);
What my code shows is that I "micro-test" the consuming object. It's like I would implement my productive code in my test. An example for the result xml is London on GeoNames.
In my opinion, it's far too granular.
But how can I mock a webservice without giving everystep? Should I let the mock object just return a XML file?
It's not about the code, but the approach.
I'm using JUnit 4.x and Mockito 1.7
I think the real problem here is that you have a singleton that calls and creates the web service so it is difficult to insert a mock one.
You may have to add (possibly package level) access to the singleton class. For example if the constructor looks something like
private GeoLocationFactory(WebService service) {
...
}
you can make the constructor package level and just create one with a mocked web service.
Alternatively you can set the webservice by adding a setter method, although I don't like mutable Singletons. Also in that case you have to remember to unset the webservice afterwards.
If the webservice is created in a method you might have to make the GeoLocationFactory extensible to substitute the mock service.
You may also look into remove the singleton itself. There are articles online and probably here on how to do that.
you really want to be mocking the results returned from the webservice to the code that will be using the result. In your example code above you seem to be mocking mDocument but you really want to pass in an instance of mDocument that has been returned from a mocked instance of your webservice and assert that the locationBean returned from the geoLocationFetcher matches the value of A_LOCATION_BEAN.
The easiest option would be to mock the WebService client,
when(geoLocationFetcher.extractGeoLocationFromXml(anyString()))
.thenReturn("<location/>");
You can modify the code to read the response xml from the file system.
Sample code can be found here: Mocking .NET WebServices with Mockito