Using a delegate twice inside of a unit test - delegates

I have some unit tests where I’m using the DoesNotThrow feature of Nunit. Up until recently, I wasn’t concerned w/ the actual response coming back. However, I recently needed to add a check for the count on my result set. I now have a redundant call in my code. Can I clean this up and make use of the delegate within my second Assert call?
Assert.DoesNotThrow(() => new GetMyCollection.GetCollection(request));
var response = new GetMyCollection().GetCollection(request);
Assert.Greater(response.MyCount, 0);

Just get rid of the first line entirely:
var response = new GetMyCollection().GetCollection(request);
Assert.Greater(response.MyCount, 0);
If new GetMyCollection().GetCollection(request) throws an exception, that will make the test fail anyway.
Assert.DoesNotThrow is relatively pointless, IMO - I would only usually use it to contrast with some Assert.Throws calls.

Related

Issues adding NEAR tokens in assemblyscript smart contract using u128.add() function

I have been facing issues with the use of u128.add(a, b) function. The two u128 values do not get added and I am afraid I am doing something wrong. I have checked LEARN-NEAR github page for sample projects and even changed my code to follow the patterns used, however the values don't get added.
signWithFunds(amount: u128): bool {
assert(context.sender, "Petition must be signed by an account");
assert(this.isFunded, `not a funded petition`);
assert(u128.ge(amount, this.minFundAmount),
`amount provided is less than minimum funding amount: at least ${asNEAR(this.minFundAmount)} required to sign this petition`);
const currentTotal = this.funding;
this.funding = u128.add(amount, currentTotal);
this.signature.push(context.sender);
return true;
}
model.ts
main.ts
aspect test file
test result showing unexpected behaviour
From the images, it looks like the test aren't receiving the expected values. The test receives 0, but expected some other values. I don't think there's anything wrong with the u128add function in itself.
From the test, you are calling a function that relies on Context's deposit, I think you need to add that to your test *(it("should sign a funded ...."), as well:
VMContext.setAttached_deposit(someDeposit)
Second, signWithFunds is relies on this.founding as well, which I believe is the funding in the petition itself. Maybe petitions[0] in your test isn't the newly created petition? We need to look at the beforeEach function to make sure, because otherwise, you are adding a new petition to the array, but you're referencing an older one.
I discovered that the 7th line of signWithFunds should have been this.funding.set(u128.add(amount, currentTotal));

How does one configure VSTS specific load test context parameters for own azure agents intelligently

Recently moved from utilising AWS to Azure for the location of our load test agents, thus making the transition to making full use of VSTS.
It was described that, for the moment, to get a load test file working with VSTS to using our own VMs for testing, we need to provide two context parameters, UseStaticLoadAgents and StaticAgentsGroupName in each loadtest file.
Our load test solution is getting very large, and we have multiple loadtest files where we have to set these two values each time. This leads us into the situation where, if we were to change our agents group name for example, we would have to update each individual load test file with the new information.
Im looking at a way to centralise this until a nicer way is implemented by Microsoft. The idea was to use a load test plugin, to add these context parameters with the plugin drawing the needed values from a centralised config file.
However, it seems that none of the hooks in the load test plugin or simply using the initialise method to manually set these values is working. Likely because they are set after full initialisation.
Has anyone got a nice, code focused solution to manage this and stop us depending on adding brittle values in the editor? Or even gotten the above approach to work?
The loadtest file is the XML file, so you can update it programmatically, for example:
string filePath = #"XXX\LoadTest1.loadtest";
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "http://microsoft.com/schemas/VisualStudio/TeamTest/2010");
XmlNode root = doc.DocumentElement;
XmlNode nodeParameters = root.SelectSingleNode("//ns:RunConfigurations/ns:RunConfiguration[#Name='Run Settings1']/ns:ContextParameters", nsmgr);
if(nodeParameters!=null)
{
//nodeParameters.SelectSingleNode("//ns:ContextParameter[#Name='UseStaticLoadAgents']").Value = "agent1";
foreach (XmlNode n in nodeParameters.ChildNodes)
{
switch (n.Attributes["Name"].Value)
{
case "Parameter1":
n.Attributes["Value"].Value = "testUpdate";
break;
case "UseStaticLoadAgents":
n.Attributes["Value"].Value = "agent1";
break;
case "StaticAgentsGroupName":
n.Attributes["Value"].Value = "group1";
break;
}
}
}
doc.Save(filePath);

Move Method Refactoring in Eclipse, programmatically, with MoveInstanceMethodProcessor cannot find a target

I am trying to automate refactoring tasks. While some refactoring tasks work with reusage of refactoring API (LTK and JDT-based), Move Method doesn't. Am I missing something with this code:
MoveInstanceMethodProcessor processorMM = new MoveInstanceMethodProcessor(method,null);
processorMM.setTargetName(method.getDeclaringType().toString());
IVariableBinding[] candidateTargets = processorMM.getCandidateTargets();
IVariableBinding[] targets= processorMM.getPossibleTargets();
processorMM.setTarget(targets[1]);
MoveRefactoring refactoringMM = new MoveRefactoring(processorMM);
RefactoringStatus statusMM = refactoringMM.checkAllConditions(monitor);
Change changeMM = refactoringMM.createChange(monitor);
undoChange = changeMM.perform(monitor);
These candidateTargets and targets are always empty, and no possible target can be found even if I loop through the all methods in the project. Some method can be moved manually, but my code doesn't find them.
Am I missing something? What is the proper way of getting target for Move Method refactoring?
NOTE: I want to avoid, as much as possible, writing my own code for anything that I can feel is already there.

Async/Await - not awaiting the async method [duplicate]

This question already has answers here:
Am I right to ignore the compiler warning for lacking await for this async call?
(3 answers)
Closed 7 years ago.
Below is my code. Compiler gives warning because AddLog is not awaited. I do not want to await this call and want to continue executing next lines. I dont have any concern if the exception is consumed also. Is it fine to ignore the warning?
public async Task Add()
{
this.AddLog( "Add executing" );
// Logic to Add Customer
}
public async Task AddLog( string message )
{
// Write to DB
}
Assuming you truly want to call the AddLog method in a fire-and-forget way, then you have a few options.
If, by design, you want AddLog to always be invoked as a fire-and-forget method, then you could change the signature to not return a Task.
public async void AddLog( string message ) // change Task to void
{
// Write to DB
// WARNING: Make sure that exceptions are handled in here.
}
However, if you do this, you better make sure that exceptions are properly handled from within the AddLog method. If any exception goes unhandled, it will crash your process.
Another option is to change the way you invoke AddLog to clearly state your intent that you don't care about when the Task completes, or about any exceptions that may be raised. You can do this by defining an empty continuation (Well, almost empty. See my edit at the bottom of the post for why it's a good idea to read the Task.Exception property at the very least).
// see EDIT for why the Task.Exception property is read here.
this.AddLog("Add executing").ContinueWith(t => { var observed = t.Exception; });
With either option, unless you are awaiting on other code inside your Add method that you are not showing us, then there is no longer any point in defining your Add method as async. You can simply turn it into a regular synchronous method. Otherwise, you'll then get another warning telling you that This async method lacks 'await' operators and will run synchronously....
public void Add() // no need for "async Task"
{
// see EDIT for why the Task.Exception property is read here.
this.AddLog("Add executing").ContinueWith(t => { var observed = t.Exception; });
// Logic to Add Customer
}
In any case, I wouldn't simply ignore the warning. Much like sometimes we get the warning Use of unassigned local variable 'x' in cases where we know that our code is fine, we typically don't ignore the warning. Instead, we may explicitly initialize the variable to null just to make our intent clear, and make the warning go away. Similarly, you can make the warning go away by making your intentions more explicit to the compiler using one of the above options.
EDIT: Word of caution about unobserved exceptions
I should also mention that even with the ContinueWith option, you may have to be careful about unhandled exceptions that come from your AddLog method.
According to this article, the way unobserved exceptions from tasks are handled has changed between .NET 4.0 and .NET 4.5. So, if you are still running .NET 4.0, or if you forcing .NET 4.0 exception behavior via configuration, you run the risk that unhandled exceptions will crash your process whenever the task gets GC-collected and finalized.
To make sure that this is not a problem, you can adjust the continuation to explicitly observe the exception, if any is present. You don't actually need to do anything with it, you just need to read it. This is one way to do it safely:
this.AddLog("Add executing").ContinueWith(t => { var observed = t.Exception; });
I've updated my earlier examples above to use the safer version of the continuation.
I would make add() non async since it isn't...and then task.run on add log

mvc3 dapper parameter issue

I have dapper working correctly, but it is unsecure as in I haven't been using parameters, how can I best turn my dapper variables into parameters for instance this is the unparameterized code that I had that worked..
var getinfo = sqlConnection.Query<test>("Select name,location from tests where location="+ myplace).FirstOrDefault();
myplace is a textbox that users put information on, now when I tried to parameterized that code like
var getinfo = sqlConnection.Query<test>("Select name,location from tests where location='#location'", new {location = myplace}).FirstOrDefault();
I get absolutely no returns back, yet no error messages. What can I be missing here or whats the best way to parameterized variables.
You do not need to place the single quotes around the parameter. Hope this helps.
var getinfo = sqlConnection.Query<test>("Select name,location from tests where location=#location", new {location = myplace}).FirstOrDefault();

Resources