Issue with using TestContext.Datarow in VSCode - c#

This is my first question here and I am hoping I will be able to receive some help.
To preface what I am trying to do is run a Data Driven test script using MSTest on VSCode.
When I attempt to get the value from the file by using
string webSiteTwo = TestContext.DataRow["Website"];
DataRow is showing an error saying:
'TestContext' does not contain a definition for 'DataRow' and no
extension method 'DataRow' accepting a first argument of type
'TestContext' could be found (are you missing a using directive or an
assembly reference?)
When looking online the DataRow object seems to come from System.Data so I added using System.Data to my program to see if that settled it, but that did not work. I then tried to add using System.Data.Datarow to see if that worked but it seems that I do not have the assemblies for that.
I was wondering if anyone has run into this problem and if they have how did they fix it.
I am using a Macbook Pro, with VSCode 1.20.1, C#
.csproj file includes these References.
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0"/>
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0"/>
<PackageReference Include="MSTest.TestFramework" Version="1.2.0"/>
<PackageReference Include="Selenium.WebDriver" Version="3.10.0"/>
<PackageReference Include="Appium.WebDriver" Version="3.0.0.2"/>
<PackageReference Include="System.Data.Common" Version="4.3.0"/>
I have set up both the Datasource and
private TestContext testContextInstance;
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}

According to the docs, the correct Namespace is:
using Microsoft.VisualStudio.TestTools.UnitTesting;
Take a look here: TestContext.DataRow

Related

ActiproSoftware controls breaks Moq unit tests

Moq does not want to work with ActiroSoftware on net core 3.1
I'm having the following issue: creating a net core 3.1 project with the following structure:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="4.14.7" />
<PackageReference Include="Actiprosoftware.Controls.WPF" Version="20.1.0" />
</ItemGroup>
</Project>
then in Program.cs try to write the following:
using Moq;
namespace moqtest
{
class Program
{
static void Main(string[] args)
{
var q = It.IsAny<string>();
}
}
}
Note that this won't compile, due to the following error:
error CS0234: The type or namespace name 'IsAny' does not exist in the namespace 'It' (are you missing an assembly reference?)
Furthermore, specifying the namespace implicitly, will work:
var q = Moq.It.IsAny<string>();
I have looked into msbuild diagnostics and it seems everything is compatible with netcoreapp3.1, but when you compile it, it seems it does not recognize It class anymore.
Please help!
Open up View > Object Browser, and search for It. You'll notice that first result is a namespace called It brought in by ActiproSoftware.BarCode.Wpf.dll
It also happens to be an empty namespace, but that's irrelevant. If it did contain anything under it, you'd refer to them as It.Something. So what happens now is that, even after you do using Moq, It is still ambiguous to the compiler.
The presence of that silly empty namespace is what's forcing to qualify your calls with Moq.

Weird version conflict: The type 'CrontabSchedule' exists in both 'NCrontab.Signed, Version=3.2.20120.0' and 'NCrontab, Version=3.2.20120.0'

I am trying do a very simple thing in console app:
using Microsoft.Azure.WebJobs.Extensions.Timers;
using NCrontab;
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
var nCrontabSchedule = CrontabSchedule.Parse("5 4 * * * *");
}
}
}
But it gives me this error:
Severity Code Description Project File Line Suppression State
Error CS0433 The type 'CrontabSchedule' exists in both 'NCrontab.Signed, Version=3.2.20120.0, Culture=neutral, PublicKeyToken=5247b4370afff365' and 'NCrontab, Version=3.2.20120.0, Culture=neutral, PublicKeyToken=null' ConsoleApp3 C:\Users\bowmanzh\source\repos\ConsoleApp3\Program.cs 12 Active
It didn't even finish compile! It’s hard for me to understand what’s going on here, I just want to use a simple thing.
Why is there a version conflict here? In fact, I referenced two packages, this is my .csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.6" />
<PackageReference Include="NCrontab" Version="3.2.0" />
</ItemGroup>
</Project>
I have already googled, they say something like Aliases, but in fact on my side it doesn't have this property.
I have already tried:
using v2 = NCrontab;
using System;
using Microsoft.Azure.WebJobs.Extensions.Timers;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
v2.CrontabSchedule nCrontabSchedule = v2.CrontabSchedule.Parse("5 4 * * *");
}
}
}
But it didn't work.
How can I solve this problem?
Thank you so much for taking the time to check!
I encountered this same problem. Trying unstalling the NCrontab nuget package and installing NCrontab.Signed instead. This resolved the issue for me.

c# editorconfig CA1062 null check validation methods (for guard clauses) with nullable reference types

I have created a small dotnetstandard 2.1 library project in a solution. I want to test out using Nullable Reference Types. As part of this, I want to have appropriate compilation errors and warnings.
TLDR;
I want to know how to setup the CA1062 code quality settings in .editorconfig correctly.
Library Project
I have added the following nuget packages to the project:
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Ardalis.GuardClauses" Version="1.4.2" />
</ItemGroup>
This includes the various code analysis packages and also Steve Smith's nice Guard Clauses library.
Nullable Reference Types has been enabled using <Nullable>enable</Nullable> in the project.
And I have a class which in the real world would be a nice implementation that actually did something:
using System;
using MyGuards;
namespace EditorConfigIssues
{
public static class TestEditorConfig
{
public static void TestMethod(MyParam input)
{
string x = input.Check;
Console.WriteLine(x);
}
}
public class MyParam
{
public MyParam(string check) => Check = check;
public string Check { get; }
}
}
Guard Library Project
In the solution I have added a simple Guard library which is another dotnetstandard 2.1 project.
using System;
namespace MyGuards
{
public static class FakeGuard
{
public static void Validate(object obj)
{
if (obj == null)
throw new ArgumentNullException();
}
}
}
NOTE: This is not in competition of the GuardClauses library - just using to contrast editorconfig with!
.editorconfig
I have added an .editorconfig to the root of solution with the following diagnostic checks:
dotnet_diagnostic.CA1062.severity = error
dotnet_code_quality.CA1062.exclude_extension_method_this_parameter = true
So with this in place, when I compile I get the following:
So everything is as I expect so far. I am not using any guards yet.
Fixing it up - Steve Smith's Guard Library
So lets try and implement the guard clauses from Steve Smiths Guard Library to get rid of the error.
So we add the following to TestEditorConfig.TestMethod:
Guard.Against.Null(input, nameof(input));
and tweak the .editorconfig with:
dotnet_code_quality.CA1062.null_check_validation_methods = Ardalis.GuardClauses.Guard.Against.Null
Excellent, all is good. The error has disappeared.
Fixing it up - my own guard library
But now I want to use my own guard. So we replace the initial guard check in TestEditorConfig.TestMethod with:
FakeGuard.Validate(input);
and replace the null_check_validation_methods in .editorconfig with:
dotnet_code_quality.CA1062.null_check_validation_methods = FakeGuard.Validate
The error is now back.
Question(s)
The question is, what do I need in order to use a project with guards from the same solution?
Why am I getting warnings for the other CA1062 in the Error Window?
The keyword "dotnet_code_quality.CA1062.exclude_extension_method_this_parameter" is unknown
The keyword "dotnet_code_quality.CA1062.null_check_validation_methods" is unknown
I have been checking out this link MS Docs Code Quality and tried various combinations for the FakeGuard, including:
MyGuards
MyGuards.FakeGuard
FakeGuard
MyGuards.FakeGuard.Validate
FakeGuard.Validate
Validate
Curiously, in a different solution, then I don't get any complaints about the CA1062 editorconfig settings in the Error Window. And in there I have been unable to get the null_check_validation_methods working - hence putting together this solution. This has been bugging me for a month or two, but finally getting the energy to put things together at the moment.
EditorConfig Bug?
If I copy the FakeGuard file to the Library project, then the error message disappears. But why does this not work in a different project in the solution.
OK... I posted on the roslyn-analyzers issues - here - https://github.com/dotnet/roslyn-analyzers/issues/3451
As it turns out this is a bug. For now here is the suggested workaround:
using System;
[AttributeUsage(AttributeTargets.Parameter)]
internal sealed class ValidatedNotNullAttribute : Attribute { }
namespace MyGuards
{
/// <summary>
/// Checks stuff.
/// </summary>
public static class FakeGuard
{
/// <summary>
/// No more nulls.
/// </summary>
/// <param name="obj"></param>
public static void Validate([ValidatedNotNull] object obj)
{
if (obj == null)
throw new ArgumentNullException();
}
}
}

EventHubTrigger C# with eventData object does not work

after I followed the instructions on the following articles
https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-hubs
I have created an EventHubTrigger, which looks like this:
using System;
public static void Run(string myEventHubMessage, ILogger log)
{
log.LogInformation($'C# Event Hub trigger function processed a message: {myEventHubMessage}');
}
This did work without any problems, but since I do need additional meta information, I changed the code to the following (described in the second linked article):
#r 'Microsoft.ServiceBus'
using System.Text;
using System;
using Microsoft.ServiceBus.Messaging;
public static void Run(EventData myEventHubMessage, ILogger log)
{
log.LogInformation($'EnqueuedTimeUtc={myEventHubMessage.EnqueuedTimeUtc}');
log.LogInformation($'SequenceNumber={myEventHubMessage.SequenceNumber}');
log.LogInformation($'Offset={myEventHubMessage.Offset}');
}
But this code results in the following error messages (btw I have also tied to use the deprected TraceWriter instead of ILogger to exactly follow the article but this results in the same error)
2018-10-11T14:22:24.814 [Error] run.csx(1,1): error CS0006: Metadata file 'Microsoft.ServiceBus' could not be found
2018-10-11T14:22:24.903 [Error] run.csx(4,17): error CS0234: The type or namespace name 'ServiceBus' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
My question is now, does anyone have a clue what to do in order to get this small piece of code running?
Of course it has to have something to do with the assemblies but the aricle states, that when working in the online portal-editor, there are no further steps to do,.
Man thanks in advance
Felix
PS:
host.json :
{
"version": "2.0"
}
Content of extensions.csproj is:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<WarningsAsErrors />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="3.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="3.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="1.0.1" />
</ItemGroup>
</Project>
Well, the sample is for function 1.x. After 2.x is generally available the function we create is on ~2 runtime by default, as we can see "version":"2.0" in host.json.
Have a try at code below, metadata is stored in SystemProperties of Microsoft.Azure.EventHubs.EventData.
#r "../bin/Microsoft.Azure.EventHubs.dll"
using System;
using Microsoft.Azure.EventHubs;
public static void Run(EventData myEventHubMessage, ILogger log)
{
log.LogInformation($"EnqueuedTimeUtc={myEventHubMessage.SystemProperties.EnqueuedTimeUtc}");
log.LogInformation($"SequenceNumber={myEventHubMessage.SystemProperties.SequenceNumber}");
log.LogInformation($"Offset={myEventHubMessage.SystemProperties.Offset}");
}
Also note that we need to use double quotation " for string in C#, see ' in your code.

Working with Mongo in .Net Core web api

I have a working Mongo and .Net Core app, but still need to access the mongo DB through C#. I am just testing this by trying to make the connection in the program.cs file. At the top I have:
using MongoDB.Driver;
using MongoDB.Driver.Core;
using MongoDB.Bson;
When I run:
var mongo = new MongoClient("mongodb://localhost:27017");
var db = mongo.GetDatabase("cvpz");
mongo.getCollection("people");
I get this error:
Program.cs(16,19): error CS1061: 'MongoClient' does not contain a definition for 'getCollection' and no extension method 'getCollection' accepting a first argument of type 'MongoClient' could be found (are you missing a using directive or an assembly reference?) [/src/src/Identity.api/Identity.api.csproj]
Now, I can access the DB through the commandline through 'mongo localhost/cvpz'. Btw, I am using Ubuntu to run .Net Core.
When I run createCollection() I get a similar error. How do I use C# to interact with Mongo?
One last thing, I should have all the necessary packages, I have these in my .csproj:
<PackageReference Include="MongoDB.Driver" Version="2.3.0" />
<PackageReference Include="MongoDB.Driver.Core" Version="2.3.0" />
<PackageReference Include="MongoDB.Bson" Version="2.3.0" />
Thanks so much guys!
Two things. 'GetCollection' should have an uppercase G. It also requires a generic parameter indicating the document type being stored. For you example:
var mongo = new MongoClient("mongodb://localhost:27017");
var db = mongo.GetDatabase("cvpz");
var coll = mongo.GetCollection<People>("people");
Reference: IMongoDatabase.GetCollection

Categories

Resources