Ask a Question


Are there any examples of using SpecsFor with NUnit's RowTests? (Now called TestCase, I believe) I'd like to replace some SpecFlow (ewww) tests with SpecsFor, but they're making heavy use of the table inputs. The modern NUnit equivalent seems to be the TestCase attribute, but where would it go? It's normally used in place of the [Test] attribute, but the parameters really should be going to the When method. I could just ditch the When, use TestCase and be done with it, but I guess I'm looking for the "yup, that's how you do it" here.

How to upgrade SpecFor to use the Latest NUnit

Want to run tests in VSTS. No tests seems to be running.

Using ConfigureContainer to pass an IEnumerable<T> isn't playing nice

I'm trying to write a spec for a class that takes as its ctor parameter an IEnumerable<T> object. I'm overriding ConfigureContainer and specifying a List<T> of real objects however the sut isn't seeing those values. Any ideas?


I have a test I need to write that is for a static method on a static class. Since I don't need a SUT to be created, I don't really have anything to put between the angle brackets on the base class. I could just not inherit from SpecsFor at all, but then I have one mutant test that doesn't match the others in terms of style. I'd like to keep the pattern of overriding Given and When for consistency with my other tests. Is there an established or generally accepted pattern for this? I can inherit from SpecsFor<object> and it doesn't seem to complain, but can I count on this in the future?

Constructor will be called before calling Given()-method

Hello, I am using SpecsFor in Version 4.4.0. My application logic has a constructor and uses a Unity-Container like this: namespace SpecsFor_Unity { public class Program { static void Main(string[] args) { } public Program(object anyParameter, IUnityContainer unityContainer) { var foo = unityContainer.Resolve<IFoo>(); if (foo == null) { throw new ArgumentNullException(nameof(foo)); } } public void DoSomething() { } } } My test class Looks like this: namespace SpecsFor_Unity_Test { public class UnitTest1 : SpecsFor<Program> { protected override void Given() { var unityContainer = new UnityContainer(); unityContainer.RegisterType<IFoo, Foo>(); var program = new Program(new object(), unityContainer); } protected override void When() { SUT.DoSomething(); } [Test] public void then_check_anything() { } } } The problem is: the ArgumentNullException will be thrown after resolving unity reference, because constructor is called before Given()-method. I expected that constructor of Program is called within Given()-method. What am I doing wrong? Thanks in advance! Kind regards, Stefan

Verification of list parameters

I'm trying to verify a call was made to a repository AddRange method. I can verify the method was called at all like this: ``` GetMockFor<IThingRepository>() .Verify(x => x.AddRange(It.IsAny<IEnumerable<Thing>>()), Times.Once); ``` I can verify that it was passed the expected two objects using some of Moq's partial matching like this: ``` GetMockFor<IThingRepository>() .Verify(x => x.AddRange(It.Is<IEnumerable<Thing>>(y => y.Count() == 2 && y.Any(z => z.ThingType == ThingType.Foo) && y.Any(z => z.ThingType == ThingType.Bar)))); ``` What I can't seem to do is get it to work using a more understandable partial matching syntax like this: ``` GetMockFor<IThingRepository>() .Verify(x => x.AddRange(Looks.Like(() => new[] { new Thing {ThingType = ThingType.Foo}, new Thing {ThingType = ThingType.Bar}, }))); ``` In every case, the code runs but the verify fails saying the method was never called. I looked through the documentation examples, and while they cover the partial matching functionality, they don't have an example of partial matching a collection of things like this. I know the partial matching can do hierarchies, but what about collections? I've even tried replacing the objects in the list with their own individual Looks.Likes: ``` GetMockFor<IThingRepository>() .Verify(x => x.AddRange( Looks.Like(() => new[] { Looks.Like(()=>new Thing {ThingType = ThingType.Foo}), Looks.Like(()=>new Thing {ThingType = ThingType.Bar}), }))); ``` Because of the way this particular code is written, the order of the elements being added is predictable, so I've eliminated that variable, although a partial match that could ignore ordering would be pretty awesome. What am I doing wrong here?

Installation - SpecsFor

Hi Matt: I've spent a frustrating couple of hours trying to install SpecsFor on a new test project in Visual Studion 2015. It first gave me the error that Moq or one of the dependencies was not the same one referenced in the assembly. I then ran the command Add-BindingRedirect in the package manager for the test project. Now I'm getting the message that SetUp : SpecsFor.WhenSpecificationException : An error occurred during the spec 'When' phase. -------------------------------------------------------------------- System.InvalidOperationException: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. I note that a couple of the dependencies seem to have new major versions which are not referenced by your package. I think for now I'll abandon SpecsFor and just install the latest versions of Moq, Nunit and Should... Regards, Roger

Problem with 2015/4.5

I see that there's a fork project that says it's for VS 2015 and .Net 4.6, but I'm on a project using 4.5, and I can't seem to get SpecsFor.MVC working. The regular version fails to install right, complaining that the browser drivers already exist. I can push past that by trying the install multiple times, but I still don't end up with a SpecsFor.Mvc reference in the project, even though the NuGet package seems to think it should be there. I tried the fork project, but didn't have any better luck there since it wants to reference a higher version of the System.Web.Mvc assembly. Is there anything else I should try?

Primitive dependencies

Moq has a way of specifying values for primitive dependencies. Is there any way to explain this to SpecsFor without completely taking over the SUT construction via InitializeClassUnderTest? In my case, I'm mocking out a repository that takes some paging and result limits at construction time. As such, I need to provide an integer to the constructor or face an error like "Int32 limit = Required primitive dependency is not explicitly defined"

Any advice for use with Knockout?

I'm on a project now that uses Knockout on the front-end, but I'm hopelessly addicted to SpecsFor.Mvc. I imagine that if the on-page controls follow the same naming convention as MVC would have, then SpecsFor would still be able to find them and use them. As I understand it from the MvcTemplates package, the magic is in wrapping a span around the controls and naming it after the property on the viewmodel. Is this correct? Are there any examples of workable patterns for integrating with Knockout? How about other front-end technologies like Angular?