|
It looks like you've chosen a really bad video to follow. That looks more like VB6 code than VB.NET.
Assuming you're actually using VB.NET, try some VB.NET tutorials instead:
Visual Basic development with Visual Studio - Visual Studio | Microsoft Docs[^]
The obvious problem is that you're putting variables and functions directly within the code file. In VB.NET, they need to be within a Class or Module . Looking at the code you've posted, it needs to be within the Class that defines your form.
NB: If you're actually using VB6, then don't! VB6 has been effectively "dead" for almost 20 years now.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks a lot!
And yes, you are right I am using VB.NET.
|
|
|
|
|
Hi,
I am writing unit tests for my application, I have a Service call which I wanna mock, the my method takes list of some type and converts that type into another type of objects then calls another back-end Service which we have only Proxy class but don't know much about it, for each item in the list - means for each item in the list we pass, it will be converted and then passed to that black-box service that we don't know anything about, our method returns nothing, I mean this methods is void method.
If Service call fails for any one item in the list, our our method throws Exception aborts calls to all the remaining items, that's all it does, its simple for implementation but for writing Unit Tests I am having problems.
What I have to check is, weather the conversion of the list of type items that we are passing is done properly or not, and there no response, so I have to mock it, I am trying, but the problem is, how am I going to check for all the items, since the scope of those items that are converted would be within that method call. Its just breaking my head, and I don't want to repeat the mock calls more than what it needs to be. Any help would be greatly greatly helpful - thanks in advance.
My Test method that I am trying:
[TestMethod]
public async Task ServiceAgreementUpdateAddTest()
{
var serviceAgreementCharacteristicRequests = new List<ServiceAgreementCharacteristicRequest>();
foreach (var serviceAgreementId in this.serviceAgreementIds)
{
var request = new ServiceAgreementCharacteristicRequest
{
ServiceAgreementId = serviceAgreementId,
CharacteristicType = characteristicType,
CharacteristicValue = characteristicValue,
ActionType = RequestAction.ADD
};
serviceAgreementCharacteristicRequests.Add(request);
}
this._serviceWrapper.Setup(e => e.UseServiceAsync(It.IsAny<Func<ATC1ServiceAgreementUpdatePortType, Task<C1ServiceAgreementUpdateResponse>>>())).ReturnsAsync(new C1ServiceAgreementUpdateResponse());
await this._service.ServiceAgreementCharacteristicAsync(serviceAgreementCharacteristicRequests).ConfigureAwait(false);
this._serviceWrapper.Verify(sw => sw.UseServiceAsync(It.IsAny<Func<ATC1ServiceAgreementUpdatePortType, Task<C1ServiceAgreementUpdateResponse>>>()), Times.Exactly(serviceAgreementCharacteristicRequests.Count));
}
My Method is as below:
public async Task ServiceAgreementCharacteristicAsync(List<ServiceAgreementCharacteristicRequest> serviceAgreementCharacteristicRequests)
{
foreach (var item in serviceAgreementCharacteristicRequests)
{
var request = new C1ServiceAgreementUpdateRequest
{
C1ServiceAgreementUpdate = new C1ServiceAgreementUpdate
{
serviceAgreement = item.ServiceAgreementId,
saChar = new[]
{
new C1ServiceAgreementUpdateSaChar
{
action = MapActionType(item.ActionType),
actionSpecified = true,
serviceAgreement11 = item.ServiceAgreementId,
characteristicType3 = item.CharacteristicType,
characteristicValue2 = item.CharacteristicValue,
effectiveDate6 = DateTime.Now,
effectiveDate6Specified = true,
searchCharacteristicValue = SearchCharacteristicValue
}
}
}
};
await this._serviceWrapper.UseServiceAsync((ATC1ServiceAgreementUpdatePortType service) => service.C1ServiceAgreementUpdateAsync(request)).ConfigureAwait(false);
}
}
private listAction MapActionType(RequestAction requestAction)
{
var actionType = listAction.add;
switch (requestAction)
{
case RequestAction.ADD:
break;
case RequestAction.UPDATE:
actionType = listAction.update;
break;
case RequestAction.DELETE:
actionType = listAction.delete;
break;
default:
throw new ArgumentException($"CustomerServiceWeb.Services.ServiceAgreements.ServiceAgreementUpdateProxy.MapActionType: Unmapped CustomerServiceWeb.Domain.Services.Common.RequestAction value: {requestAction.ToString()}");
}
return actionType;
}
modified 8-Feb-19 15:56pm.
|
|
|
|
|
You "mock" the service call, capture the "send" (as XML or whatever), print it or display it, compare it to an "expected output".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
But I am passing args as list to the method that I am going to test but that method loops through each individual item of the list and converts it then calls the service by sending that converted object as an argument.
The problem is how am I going to know the converted object when it is within the scope of the method that I am testing.
You kind of gave me definitely some hint, maybe I am not able to pick it up very well, any code snippet or example link etc, anything helps my friend.
It seems like it is difficult to test these types of Black-box methods (which return void) - and thanks a lot for the support my friend.
One of my Colleague has provided me the below method to do it:
public async Task ServiceAgreementUpdateAddTest()
{
var expectedServiceRequest1 = new C1ServiceAgreementUpdateRequest();
var expectedServiceRequest2 = new C1ServiceAgreementUpdateRequest();
var serviceAgreementCharacteristicRequests = new List<ServiceAgreementCharacteristicRequest>();
foreach (var serviceAgreementId in this.serviceAgreementIds)
{
var request = new ServiceAgreementCharacteristicRequest
{
ServiceAgreementId = serviceAgreementId,
CharacteristicType = characteristicType,
CharacteristicValue = characteristicValue,
ActionType = RequestAction.ADD
};
serviceAgreementCharacteristicRequests.Add(request);
}
this._serviceWrapper.Setup(e => e.UseServiceAsync(It.IsAny<Func<ATC1ServiceAgreementUpdatePortType, Task<C1ServiceAgreementUpdateResponse>>>()))
.Callback((Func<ATC1ServiceAgreementUpdatePortType, Task<C1ServiceAgreementUpdateResponse>> serviceFunc) => { serviceFunc.Invoke(this._serviceAgreementUdateSvc.Object); })
.ReturnsAsync(new C1ServiceAgreementUpdateResponse());
await this._service.ServiceAgreementCharacteristicAsync(serviceAgreementCharacteristicRequests).ConfigureAwait(false);
this._serviceWrapper.Verify(sw => sw.UseServiceAsync(It.IsAny<Func<ATC1ServiceAgreementUpdatePortType, Task<C1ServiceAgreementUpdateResponse>>>()), Times.Exactly(serviceAgreementCharacteristicRequests.Count));
this._serviceAgreementUdateSvc.Verify(svc => svc.C1ServiceAgreementUpdateAsync(It.Is<C1ServiceAgreementUpdateRequest>(x =>x.Equals(expectedServiceRequest1))));
this._serviceAgreementUdateSvc.Verify(svc => svc.C1ServiceAgreementUpdateAsync(It.Is<C1ServiceAgreementUpdateRequest>(x => x.Equals(expectedServiceRequest2))));
}
But when I tried to see by putting the breakpoint there, the inside values or attributes of the expectedServiceRequest1 and expectedServiceRequest2 are coming null, I am not understanding how it is working and what is the fix for it. Any help please? Am I doing any mistake or my colleague has not provided me complete code, its spinning my head - need some help please.
modified 8-Feb-19 18:16pm.
|
|
|
|
|
I changed my Test method as below:
[TestMethod]
public async Task ServiceAgreementUpdateAddTest()
{
DateTime dateTime = DateTime.Today;
var expectedServiceRequest1 = new C1ServiceAgreementUpdateRequest
{
C1ServiceAgreementUpdate = new C1ServiceAgreementUpdate
{
serviceAgreement = this.serviceAgreementIds[0],
saChar = new[]
{
new C1ServiceAgreementUpdateSaChar
{
action = listAction.add,
actionSpecified = true,
serviceAgreement11 = this.serviceAgreementIds[0],
characteristicType3 = characteristicType,
characteristicValue2 = characteristicValue,
effectiveDate6 = dateTime,
effectiveDate6Specified = true,
searchCharacteristicValue = searchCharacteristicvalue
}
}
}
};
var expectedServiceRequest2 = new C1ServiceAgreementUpdateRequest
{
C1ServiceAgreementUpdate = new C1ServiceAgreementUpdate
{
serviceAgreement = this.serviceAgreementIds[0],
saChar = new[]
{
new C1ServiceAgreementUpdateSaChar
{
action = listAction.add,
actionSpecified = true,
serviceAgreement11 = this.serviceAgreementIds[1],
characteristicType3 = characteristicType,
characteristicValue2 = characteristicValue,
effectiveDate6 = dateTime,
effectiveDate6Specified = true,
searchCharacteristicValue = searchCharacteristicvalue
}
}
}
};
var serviceAgreementCharacteristicRequests = new List<ServiceAgreementCharacteristicRequest>();
foreach (var serviceAgreementId in this.serviceAgreementIds)
{
var request = new ServiceAgreementCharacteristicRequest
{
ServiceAgreementId = serviceAgreementId,
CharacteristicType = characteristicType,
CharacteristicValue = characteristicValue,
ActionType = RequestAction.ADD
};
serviceAgreementCharacteristicRequests.Add(request);
}
this._serviceWrapper.Setup(e => e.UseServiceAsync(It.IsAny<Func<ATC1ServiceAgreementUpdatePortType, Task<C1ServiceAgreementUpdateResponse>>>()))
.Callback((Func<ATC1ServiceAgreementUpdatePortType, Task<C1ServiceAgreementUpdateResponse>> serviceFunc) => { serviceFunc.Invoke(this._serviceAgreementUdateSvc.Object); })
.ReturnsAsync(new C1ServiceAgreementUpdateResponse());
await this._service.ServiceAgreementCharacteristicAsync(serviceAgreementCharacteristicRequests).ConfigureAwait(false);
this._serviceWrapper.Verify(sw => sw.UseServiceAsync(It.IsAny<Func<ATC1ServiceAgreementUpdatePortType, Task<C1ServiceAgreementUpdateResponse>>>()), Times.Exactly(serviceAgreementCharacteristicRequests.Count));
}
I am getting the following error:
Failed ServiceAgreementUpdateAddTest
Error Message:
Test method CustomerServiceWeb.Services.Test.ServiceAgreements.ServiceAgreementUpdateProxyTest.ServiceAgreementUpdateAddTest threw exception:
Moq.MockException:
Expected invocation on the mock once, but was 0 times: svc => svc.C1ServiceAgreementUpdateAsync(It.Is<C1ServiceAgreementUpdateRequest>(x => x.Equals(C1ServiceAgreementUpdateRequest)))
Configured setups:
s => s.C1ServiceAgreementUpdateAsync(It.IsAny<C1ServiceAgreementUpdateRequest>())
Performed invocations:
ATC1ServiceAgreementUpdatePortType.C1ServiceAgreementUpdateAsync(C1ServiceAgreementUpdateRequest)
ATC1ServiceAgreementUpdatePortType.C1ServiceAgreementUpdateAsync(C1ServiceAgreementUpdateRequest)
Stack Trace:
at Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable`1 setups, IEnumerable`1 actualCalls, Expression expression, Times times, Int32 callCount) in C:\projects\moq4\Source\Mock.cs:line 473
at Moq.Mock.VerifyCalls(Mock targetMock, MethodCall expected, Expression expression, Times times) in C:\projects\moq4\Source\Mock.cs:line 452
at Moq.Mock.Verify[T,TResult](Mock`1 mock, Expression`1 expression, Times times, String failMessage) in C:\projects\moq4\Source\Mock.cs:line 340
at Moq.Mock`1.Verify[TResult](Expression`1 expression, Func`1 times) in C:\projects\moq4\Source\Mock.Generic.cs:line 508
at CustomerServiceWeb.Services.Test.ServiceAgreements.ServiceAgreementUpdateProxyTest.<ServiceAgreementUpdateAddTest>d__8.MoveNext() in C:\SourceCode\FlexPricing\CustomerServiceWeb.Services.Test\ServiceAgreements\ServiceAgreementUpdateProxyTest.cs:line 115
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action)
Any help what am I missing?
|
|
|
|
|
That's the "problem" with frameworks: someone else's will on your own ways.
You need a "service stub". You have too much plumbing mixed with app code.
The "service" is app code; the "call" to the server (and its housekeeping) is plumbing.
You create a module to call the service; another that doesn't "call" but uses the "data for the call" as part of the test.
(Previously you queried the server and captured the "expected result" to be used to compare in the unit tests).
You now have input and outputs. You "swap in" (inject) the "service caller" depending on live versus test.
That's it without all the code bloat.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Hi,
I am writing some Exception handling to my class, when the Exception is thrown and message or inner exception is not giving us proper information, I have digged into the Detail property of the Exception instance, in the detail under the Nodes, the proper exception is shown in last node. Now I want to log this value, but when I am trying to get the artifacts of the Detail property of the Exception I am getting the error saying Detail is not a property of method of the object Exception.
Please help me how can I read the values of Nodes of the Exception Detail or make this as the Exception message and re-throw the exception if the message or inner-exception values are not correct. Any help would be greatly appreciated.
((System.ServiceModel.FaultException<CustomerServiceWeb.Services.ServiceAgreementUpdateSvc.ouaf.oracle.com.Fault>)ex).Detail.Nodes
|
|
|
|
|
Show your exception handling code. I get the feeling you did something like this
catch (Exception ex)
{
...
}
Correct?
|
|
|
|
|
Yes I did, but for some reason the Detail property is visible in the quick watch but when I try to write it by using the intellisense and get those on the screen or some where in the log I am not able to access it, any advice would be very very appreciated, try it, you will have a detail property and try to write them on the screen or some where to persist, you will not find it in the code to write it.
|
|
|
|
|
The Exception class doesn't have a Details property, so you're code can't see it. You're basically casting the exception object back up to the root Exception class, stripping all the properties that don't exist in the Exception class.
If you want to catch a specific type of exception, you have to put that type in the catch statement:
try
{
}
catch (SomeSpecificExceptionClass ex)
The QuickWatch window uses the actual type of the class to fill in all available properties. That's why you see it in QuickWatch but the code can't.
|
|
|
|
|
Anyways to bring that up into the code of the catch block?
|
|
|
|
|
Change your exception handler to the type shown in the quick watch.
This space for rent
|
|
|
|
|
|
Yes thanks my friend, just trying to get work around if any, since the Inner Exception and Message properties both are coming null from the Service.
Yeah I agree, you did give me idea what it is actually and thank you very much for the help.
|
|
|
|
|
|
Yeah I understand that, but I am seeing the Detail property in the Quick-watch but not in the intellisense and while trying to code the values, just try it, you can understand, I don't know why is it happening.
|
|
|
|
|
You just need to change your code to display all the information that the exception provides. As I said previously, look at the documentation for what is available.
|
|
|
|
|
|
I have 2 .net forms, each with their own BindingSource which I set to go after the same DataTable. The table is filled early on with 35 rows. One of the BindingSources has a filter set when the form loads, which makes the result set 4 rows. When I go back to the first form, the row count has gone down to 4 - if I set that BindingSource filter to "", the count returns to 35.
It looks as if the two binding sources are sharing stuff, where changing the filter on one affects the other.
Any explanation for this? I am working around it but would like to understand the mechanism.
Bob
|
|
|
|
|
It's the sharing of the same DataSource that is the issue. When you set the Filter to a none null value, the property is passed down to the underlying data source so you are filtering on the source itself. Source[^].
This space for rent
|
|
|
|
|
Thanks, if I read the docs right, it appears if I tie the binding sources to DataViews, not the DataTable, it might work as I intend and keep the filters separate...
|
|
|
|
|
Based on my own experience, I am just need to create public static DataTable datasource which is will be consumed by that 2 or more forms...
Not just Datatable, but also collection,
We can put it on main Program.cs, let's say it glbData.
public class Program{
public static DataTable glbData;
}
usage :
DataTable dt = resultset[0];
Program.glbData = dt;
Regards
Toha
|
|
|
|
|
And how does your solution solve the problem the poster had? Go ahead and try your solution with the problem the OP had.
This space for rent
|
|
|
|
|
I have test this and the first form not affected with changes from FormMahasiswa
private void FormMahasiswa_Load(object sender, EventArgs e)
{
Form1.glbData = new DataTable();
Form1.glbData.Columns.Add("Newww", typeof(int));
Form1.glbData.Columns.Add("Drug", typeof(string));
Form1.glbData.Columns.Add("Patient", typeof(string));
Form1.glbData.Columns.Add("Date", typeof(DateTime));
Form1.glbData.Rows.Add(25, "Sample Change", "Change ", DateTime.Now);
Form1.glbData.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
Form1.glbData.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
Form1.glbData.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
Form1.glbData.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
dataGridView1.DataSource = Form1.glbData;
}
Regards
Toha
modified 17-Jan-19 3:34am.
|
|
|
|
|