How do I Skip xUnit.net test based on certain bool condition in test class? - xunit

I have a need where i need to skip the test method based on certain bool condition in a class.
is it possible? how can it be achieved? i have tried extending the FactAttribute but i cannot get the instance of the Test class.
my code below:
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
namespace XUnitTestProject1
{
public class MyTestClass
{
bool SomeCondition;
public MyTestClass()
{
SomeCondition = false;
}
[Fact] //I WANT TO SKIP THIS TEST AS SOMECONDITON == FALSE
void MyTestMethod()
{
}
}
}

You can do something like this:
public class MyTestClass
{
private const string SomeCondition = "false"
[Fact(Skip=SomeCondition)]
void MyTestMethod()
{
}
}

Related

Autofac asp.net web api constructor injection works but property injection does not

I am beginner in autofac and I have to use it in new legacy project asp.net web api.
I am registering of interface and injection works fine with constructor injection.
However, the constructor is being called in numerous places directly new(), and I don't want to replace it everywhere.
So I thought about property injection, but cannot get it to work, the dependency is always null.
The app is split into multiple projects and multiple autofac modules. Autofac configuration as per docs: https://docs.autofac.org/en/latest/integration/webapi.html
I tried to make small demo app, and I was able to get property injection working using all methods from docs: https://autofac.readthedocs.io/en/latest/register/prop-method-injection.html
using Autofac;
public class Program
{
public static void Main()
{
var builder = new ContainerBuilder();
builder.RegisterType<MyDependency>().As<IMyDependency>().SingleInstance();
builder.RegisterType<MyService>().OnActivated(e => e.Instance.MyDependency1 = e.Context.Resolve<IMyDependency>());
//builder.Register(c => new MyService { MyDependency1 = c.Resolve<IMyDependency>() });
//builder.RegisterType<MyService>().WithProperty("MyDependency1", new MyDependency()).SingleInstance();
var container = builder.Build();
container.Resolve<MyService>();
}
}
public class MyService
{
public IMyDependency MyDependency1 { get; set; }
}
public class MyDependency : IMyDependency
{
public void Hello()
{
Console.WriteLine("Hello from MyDependency1");
}
public MyDependency()
{
Hello();
}
}
public interface IMyDependency
{
public void Hello();
}
Unfortunately none of these works for my full project, the object is always null. I know it would be difficult to get help, but maybe someone can advice what to look for?
I just tried reproducing this using the WithProperty registration you have there and the test passes - I can't reproduce it, property injection is working.
If it's not working in your full project, something else is going on. Below is the totally working test I used to verify.
public class ExampleTests
{
[Fact]
public void PropertyInjection()
{
var builder = new ContainerBuilder();
builder.RegisterType<MyDependency>().As<IMyDependency>().SingleInstance();
builder.RegisterType<MyService>().WithProperty("MyDependency1", new MyDependency()).SingleInstance();
var container = builder.Build();
var svc = container.Resolve<MyService>();
Assert.NotNull(svc.MyDependency1);
}
}
public class MyService
{
public IMyDependency MyDependency1 { get; set; }
}
public class MyDependency : IMyDependency
{
public void Hello()
{
Console.WriteLine("Hello from MyDependency1");
}
public MyDependency()
{
Hello();
}
}
public interface IMyDependency
{
public void Hello();
}

Textcontext property in MStest giving null reference exeption

I am trying to create a Unit test project in Visual studio 2017 . I want to use Testcontext class prorperties like TestName and etc in my test class and Test method . But when i run the project in debug mode i get null object reference for Testcontext object .
Below is the code :
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject2
{
[TestClass]
public class UnitTest1
{
private TestContext _testcontext;
public TestContext Testcontext
{
get { return _testcontext; }
set { _testcontext = value; }
}
[TestMethod]
public void TestMethod2()
{
Console.WriteLine(Testcontext.TestName);
}
}
}
I am not able to find out how to fix this problem using Coded UI project it works fine.
the exception
You need to change the definition for TestContext property.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject2
{
[TestClass]
public class UnitTest1
{
public TestContext TestContext { get; set; }
[TestMethod]
public void TestMethod2()
{
Console.WriteLine(Testcontext.TestName);
}
}
}
You haven't set value for the _testcontext in the code sample you have provided so you will get NullReferenceException.

How do I resolve MarkMembersAsStatic FxCop error?

I have a method ResetMethod(ClassA a) in a class and I have accessed this method by property of ResetMethod's class like this:
public class MyClass1
{
public MyClass1()
{
}
public void ResetMethod(ClassA a)
{
}
}
public class MyClass2
{
MyClass1 class1;
public MyClass2()
{
ClassA a= new ClassA();
MyClass1.ResetMethod(a);
}
public MyClass1 MyClass1
{
get
{
if (myClass1 == null)
myClass1 = new MyClass1 ();
return myClass1 ;
}
set
{
myClass1 = value;
}
}
}
While running FxCop rules, for method ResetMethod, it shows this error:
The 'this' parameter (or 'Me' in Visual Basic) of 'MyClass1.ResetMethod(MyClassA)' is never used. Mark the member as static (or Shared in Visual Basic) or use 'this'/'Me' in the method body or at least one property accessor, if appropriate.
How do I resolve this error?
You invocation of MyClass1.ResetMethod(a); is already calling a static method. So the code you posted does not compile to my understanding.
So all that's left to the do is to make the method itself static:
public static void ResetMethod(ClassA a)
{
// ...
}

How to pass viewbag information from Repository

I am moving my crud operations from the controller into a repository.
How can I pass the viewbag information from the repository to the controller and then to the view?
My Controller calls PopulateAssigneUserData in the repository
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WWW.Models;
using WWW.DAL;
using WWW.ViewModels;
namespace WWW.Controllers
{
public class RolesController : Controller
{
private Iaspnet_RolesRepository aspnet_RolesRepository;
public RolesController()
{
this.aspnet_RolesRepository = new Aspnet_RolesRepository(new WorldofWarCraftContext());
}
public RolesController(Iaspnet_RolesRepository aspnet_RolesRepository)
{
this.aspnet_RolesRepository = aspnet_RolesRepository;
}
//
// GET: /Roles/Edit/5
public ActionResult Edit(Guid id)
{
aspnet_Roles aspnet_Role = db.aspnet_Roless
.Include(i => i.aspnet_User)
.Where(i => i.RoleId == id)
.Single();
PopulateAssigneUserData(aspnet_Role);
return View(aspnet_Role);
}
My Repository has the code that used to live in the controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using WWW.Models;
using WWW.ViewModels;
namespace WWW.DAL
{
public class Aspnet_RolesRepository : Iaspnet_RolesRepository, Iaspnet_UsersRepository , IDisposable
{
private WorldofWarCraftContext context;
public Aspnet_RolesRepository(WorldofWarCraftContext context)
{
this.context = context;
}
public IEnumerable<aspnet_Roles> GetRoles()
{
return context.aspnet_Roless.ToList();
}
public aspnet_Roles GetRolesByID(Guid id)
{
return context.aspnet_Roless.Find(id);
}
private void PopulateAssigneUserData(aspnet_Roles aspnet_Role)
{
var allUsers = context.aspnet_Users;
var rolesUsers = new HashSet<Guid>(aspnet_Role.aspnet_User.Select(c => c.UserId));
var viewModel = new List<AssigneUserData>();
foreach (var User in allUsers)
{
viewModel.Add(new AssigneUserData
{
UserId = User.UserId,
UserName = User.UserName,
Assigned = rolesUsers.Contains(User.UserId)
});
}
ViewBag.Users = viewModel;
}
You are mixing the responsibilities here. A repository should not work with view models and ViewBag. A repository should return domain models. It is the controller responsibility to map those domain models to view models and pass those view models to the view. This PopulateAssigneUserData method should not live in your repository. In your repository you should have a method that returns an aspnet_Role instance given an id (the code that you currently have in your Edit action).

Repository not implementing interface correctly

The error I am getting is
Error 1 'OCDSandbox.Models.OrganizationRepository' does not implement
interface member
'OCDSandbox.Models.IRepository.FindByOrgNbr()' C:\source
temp\OCDSandbox\OCDSandbox\Models\OrganizationRepository.cs line 9
OrganizationRespository.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace OCDSandbox.Models
{
public class OrganizationRepository : IRepository
{
private GMS_Sandbox_testDataContext _dataContext;
public OrganizationRepository()
{
_dataContext = new GMS_Sandbox_testDataContext();
}
public IList<Organization> ListAll()
{
var organizations = from o in _dataContext.Organizations
select o;
return organizations.ToList();
}
public ICollection<Organization> FindAll()
{
return _dataContext.Organizations.ToList();
}
public ICollection<Organization> FindByOrgNbr(string OrgNbr)
{
var organizations = _dataContext.Organizations.Where(p => p.org_nbr == OrgNbr).ToList();
return organizations;
}
}
}
IRepository.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace OCDSandbox.Models
{
public interface IRepository
{
// organization interface
IList<Organization> ListAll();
ICollection<Organization> FindAll(); //returns list read only
ICollection<Organization> FindByOrgNbr();
}
}
Any help is greatly appreciated. This is my first time using repo and I would like to then test FindByOrgNbr in controller. Thanks in advance!
You're missing a parameter in the interface in the definition of FindByOrgNbr
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace OCDSandbox.Models
{
public interface IRepository
{
// organization interface
IList ListAll();
ICollection FindAll(); //returns list read only
ICollection FindByOrgNbr(string OrgNbr);
}
}
Thanks because the interface has this method: ICollection<Organization> FindByOrgNbr(); and your class has public ICollection<Organization> FindByOrgNbr(string OrgNbr)
It seems the interface needs the OrgNbr parameter too.
You're missing the OrgNbr parameter in your interface definition for FindByOrgNbr().
public interface IRepository
{
// organization interface
IList<Organization> ListAll();
ICollection<Organization> FindAll(); //returns list read only
ICollection<Organization> FindByOrgNbr(string OrgNbr);
}

Resources