Discussions

Ask a Question
ANSWERED

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?
ANSWERED

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?
ANSWERED

HTTP Headers

Hello Is there a way to detect the header response, when the form is submitted? For example SUT.FindFormFor<RegistrationViewModel>() .Field(x => x.Email) .SetValueTo("[email protected]") .Field(x => x.Password) .SetValueTo("12345") .Submit(); At this point, if the response is HTTP 500, the testing goes on. "When" does not fail. Is there a way to assert that the response must be HTTP 200?
ANSWERED

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

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

Windows Authentication not working

Hi, how do you tell your framework to use windows authentication with the IIS Express host. <authentication mode="Windows" /> <authorization> <deny users="?" /> </authorization> My tests are failing, since the page returns Error message 401.2.: Unauthorized: Logon failed due to server configuration. With the authorization set to None, allow all users, it works as expected. Thanks, Stevo

SpecsFor<object?>

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?
ANSWERED

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"
ANSWERED

Telerik JustMock

Do you see any issues with using Specs for and Telerik's JustMock mocking library? We have a dependency on it for Fakes of legacy code.
ANSWERED

Theory

How does this library interact with Theories. I'd like to have multiple datapoints for my tests.