XUnit Ignore test at runtime (with SpecFlow tag @IgnoreLocally)

Code –> https://github.com/chrismckelt/XUnit.OptionallyIgnore NuGet –> https://www.nuget.org/packages/Xunit.SpecFlow.AssertSkip/ As Xunit has no Assert.Ignore() using the OptionallyIgnoreTestFactAttribute attribute on a method and setting McKeltCustom.SpecflowPlugin.Settings.IgnoreLocally == true will ignore the test at runtime In SpecFlow set this as a plugin and use the tag @IgnoreLocally– before each test scenario is run turn on/off the setting to run this test. Sample usage: namespace Tester { public class Dummy { public Dummy() { McKeltCustom.SpecflowPlugin.Settings.IgnoreLocally = true; } } public class TestFixture : IUseFixture<Dummy> { [OptionallyIgnoreTestFact\] public void DoesThisWork() { Assert.True(false, "This should not be run"); } public void SetFixture(Dummy data) { throw new NotImplementedException(); } } } SpecFlow tag can be on a feature or a scenario ...

October 2013 · Smart Tech

Sales Tax Calculator using SpecFlow & The State Pattern And Chained Commands

Using SpecFlow heres how I solved the following problem PROBLEM : SALES TAXES Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical products that are exempt. Import duty is an additional sales tax applicable on all imported goods at a rate of 5%, with no exemptions._ When I purchase items I receive a receipt which lists the name of all the items and their price (including tax), finishing with the total cost of the items, and the total amounts of sales taxes paid. The rounding rules for sales tax are that for a tax rate of n%, a shelf price of p contains (np/100 rounded up to the nearest 0.05) amount of sales tax. ...

March 2011 · Smart Tech

RhinoMocks – WhenCalled

The following test would fail without this .WhenCalled(invocation => invocation.ReturnValue = new TestResult(){IsTrue = true, Message = "BBB"}) [TestClass] public class ChrisTest { private IRuleService ruleService; [TestMethod] public void ShouldNotChangeReturnedTestResult() { ruleService = MockRepository.GenerateMock<IRuleService>(); var testResult = new TestResult(); testResult.IsTrue = true; testResult.Message = "AAA"; ruleService.Stub(a => a.GetTestResult()).Return(testResult) .WhenCalled(invocation => invocation.ReturnValue = new TestResult(){IsTrue = true, Message = "BBB"}); var testClass = new TestClass(ruleService); testClass.KillTheString(); Assert.IsTrue(testClass.StringIsThere()); } public interface IRuleService { TestResult GetTestResult(); } public class TestResult { public bool IsTrue { get; set; } public string Message { get; set; } } private class TestClass { private readonly IRuleService ruleService; public TestClass(IRuleService ruleService) { this.ruleService = ruleService; } public void KillTheString() { var result = ruleService.GetTestResult(); result.Message = string.Empty; } public bool StringIsThere() { var result = ruleService.GetTestResult(); return !string.IsNullOrEmpty(result.Message); } } }

July 2010 · Smart Tech

Rhino Mock Constraints -- AssertWasCalled

Rhino Mock Constraints allow use to test a methods parameters were called with the correct arguments. public interface IDocumentService { void Save(string userName, Document document, Stream stream); } Some ways to ensure the method that contains the save method passes the correct internally constructed arguments include documentService.AssertWasCalled(a=>a.Save(“chris”, doc, adaptor.InputStream), b => b.Constraints(Is.Equal(“chris”), Is.NotNull(), Is.AnyThing())); Passing in Property.AllPropertiesMatch(this.MyTestObjectWithPropertiesThatShouldMatch) will check values against each object

May 2010 · Smart Tech

ISpecification

public interface ISpecification<T> { bool IsSatisfiedBy(T candidate); ISpecification<T> And(ISpecification<T> other); ISpecification<T> Or(ISpecification<T> other); ISpecification<T> XOr(ISpecification<T> other); ISpecification<T> AndAllOf(IEnumerable<ISpecification<T>> specifications); T Target { get; set; } void GetResults(ResultsVisitor visitor); IEnumerable<Type> WhatWasAssessed(); Risks.IDemoCommand GetCommand(); } using System; using System.Collections.Generic; using Demo.Core.Commands; using Demo.Core.Risks; using Demo.Core.Specification.Common; namespace Demo.Core.Specification { public abstract class AbstractSpecification<T> : ISpecification<T> { protected SpecificationResult result; protected IList<Type> ruleList = new List<Type>(); protected bool SoftCheck { get; set; } public T Candidate { get; set; } public T Target { get; set; } public bool IsReplay {protected get; set;} public ISpecification<T> And(ISpecification<T> other) { return new AndSpecification<T>(this, other); } public ISpecification<T> Or(ISpecification<T> other) { return new OrSpecification<T>(this, other); } public ISpecification<T> XOr(ISpecification<T> other) { return new XORSpecification<T>(this, other); } public ISpecification<T> AndAllOf(IEnumerable<ISpecification<T>> specifications) { return new AndAllOfSpecification<T>(this, specifications); } public virtual bool Evaluate(T candidate) { throw new NotImplementedException("You need to provide a predicate or override IsSatisfiedBy"); } public virtual IDemoCommand GetCommand() { return new NoCommand(); } public virtual void GetResults(ResultsVisitor visitor) { if (result != null && !string.IsNullOrEmpty(result.Message)) { visitor.Add(result); } } public virtual IEnumerable<Type> WhatWasAssessed() { return ruleList; } public virtual string MessageFormat() { throw new NotImplementedException("You need to provide a result a message or override IsSatisfiedBy"); } public virtual bool IsSatisfiedBy(T candidate) { if (IsReplay) { return true; } Type ruleType = GetType(); ruleList.Add(ruleType); Candidate = candidate; bool satisfied = Evaluate(candidate); if (!satisfied || (SoftCheck && !string.IsNullOrEmpty(result.Message))) { result = new SpecificationResult { Satisfied = SoftCheck, Message = string.Format(MessageFormat(), candidate, Target) }; } return satisfied; } public AbstractSpecification<T> SuccessOrRhs(ISpecification<T> specification) { return new SuccessOrRhsSpecification<T>(specification); } } }

February 2010 · Smart Tech