Giter Site home page Giter Site logo

fluentassertions.mvc's Introduction

Extension methods to fluently assert the outcome of .NET tests

Coveralls branch qodana GitHub Repo stars GitHub contributors GitHub last commit GitHub commit activity open issues

A very extensive set of extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit tests. Works with .NET Standard 2.0 and higher, .NET Framework 4.7 and higher and .NET 6 and higher.

See https://www.fluentassertions.com for background information, usage documentation, an extensibility guide, support information and more tips & tricks.

Who created this?

Originally authored by Dennis Doomen with Jonas Nyrup as the productive side-kick. Notable contributions were provided by Artur Krajewski, Lukas Grützmacher and David Omid.

How do I build this?

Install Visual Studio 2022 17.8+ or JetBrains Rider 2021.3 as well as the Build Tools 2022 (including the Universal Windows Platform build tools). You will also need to have .NET Framework 4.7 SDK and .NET 8.0 SDK installed. Check global.json for the current minimum required version.

What are these Approval.Tests?

This is a special set of tests that use the Verify project to verify whether you've introduced any breaking changes in the public API of the library.

If you've verified the changes and decided they are valid, you can accept them using AcceptApiChanges.ps1 or AcceptApiChanges.sh. Alternatively, you can use the Verify Support plug-in to compare the changes and accept them right from inside Rider. See also the Contribution Guidelines.

Sponsors

     

With support from the following public sponsors

fluentassertions.mvc's People

Contributors

caseyburns avatar gimly avatar jamiehumphries avatar jrnail23 avatar kevinkuszyk avatar rikrak avatar sharpjs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

fluentassertions.mvc's Issues

The second call to WithViewData throws NullReferenceException

This code:

            const string pageName = "pageName";

            _sut.GetByPageName(pageName)
                .Should()
                .BeViewResult()
                .WithViewData("PageName", pageName)
                .WithViewData("Roles", _roles) // this call fails!
                .WithViewData("FieldName", null);

causes:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=FluentAssertions.Mvc5
  StackTrace:
   at FluentAssertions.Mvc.ViewResultBaseAssertions`1.WithViewData(String key, Object expectedValue, String reason, Object[] reasonArgs)
   at Tests.Controllers.HelpEditor.HelpEditorControllerTest.GetByPageName_Should_return_view_for_specified_pageName() in ...\....Tests\Controllers\HelpEditor\HelpEditorControllerTest.cs:line 48

Upgrade to FluentAssertions v6

Updating my project to the latest FluentAssertions library causes runtime errors:
System.MissingMethodException: Method not found: 'Void FluentAssertions.Primitives.ReferenceTypeAssertions`2.set_Subject(!0)'.
at FluentAssertions.Mvc.ActionResultAssertions..ctor(ActionResult subject)
at FluentAssertions.Mvc.AssertionsExtensions.Should(ActionResult actual)

This is due to v6 having some breaking changes, specifically around how the Assertions base classes are constructed

Could not install FluentAssertions.Mvc4 in a project targeting .NET 4.0

Please see the Package Manager error below.

I was able to successfully install a previous version using the command:
Install-Package FluentAssertions.Mvc4 -Version 0.3.0

PM> Install-Package FluentAssertions.Mvc4
Attempting to resolve dependency 'FluentAssertions (≥ 2.2)'.
Attempting to resolve dependency 'Microsoft.AspNet.Mvc (≥ 4.0.30506.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebPages (≥ 2.0.20710.0)'.
Attempting to resolve dependency 'Microsoft.Web.Infrastructure (≥ 1.0.0.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.Razor (≥ 2.0.20710.0 && < 2.1)'.
Installing 'FluentAssertions.Mvc4 0.4.0.0'.
Successfully installed 'FluentAssertions.Mvc4 0.4.0.0'.
Adding 'FluentAssertions 2.2.0.0' to test.
Installing 'FluentAssertions 2.2.0.0'.
You are downloading FluentAssertions from Dennis Doomen,   Martin Opdam, the license     agreement to which is available at    https://github.com/dennisdoomen/FluentAssertions/blob/master/LICENSE. Check the package for  additional dependencies, which may come with their own license agreement(s). Your use of the   package and dependencies constitutes your acceptance of their license agreements. If you do not  accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'FluentAssertions 2.2.0.0'.
Successfully added 'FluentAssertions 2.2.0.0' to test.
Adding 'Microsoft.Web.Infrastructure 1.0.0.0' to test.
Successfully added 'Microsoft.Web.Infrastructure 1.0.0.0' to test.
Adding 'Microsoft.AspNet.Razor 2.0.20710.0' to test.
Successfully added 'Microsoft.AspNet.Razor 2.0.20710.0' to test.
Adding 'Microsoft.AspNet.WebPages 2.0.20710.0' to test.
Successfully added 'Microsoft.AspNet.WebPages 2.0.20710.0' to test.
Adding 'Microsoft.AspNet.Mvc 4.0.30506.0' to test.
Successfully added 'Microsoft.AspNet.Mvc 4.0.30506.0' to test.
Adding 'FluentAssertions.Mvc4 0.4.0.0' to test.
Uninstalling 'FluentAssertions.Mvc4 0.4.0.0'.
Successfully uninstalled 'FluentAssertions.Mvc4 0.4.0.0'.
Uninstalling 'FluentAssertions 2.2.0.0'.
Successfully uninstalled 'FluentAssertions 2.2.0.0'.
Install failed. Rolling back...
Install-Package : Could not install package 'FluentAssertions.Mvc4 0.4.0.0'. You are trying to install       this package into a project that targets '.NETFramework,Version=v4.0', but
  the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
At line:1 char:16
+ Install-Package <<<<  FluentAssertions.Mvc4
    + CategoryInfo          : NotSpecified: (:) [Install-Package], InvalidOperationException
    + FullyQualifiedErrorId :     NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand

Problem with BeOfType and IReadOnlyList

Hi,

I seem to have a problem comparing types.
VS2013.4
FA 3.3.0

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using FluentAssertions;

namespace IReadOnlyList
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void IReadOnlyListTest()
        {
            var newOrder = new Order();

            Assert.IsInstanceOfType(newOrder.Items, typeof(IReadOnlyList<OrderItem>)); // Works fine
            newOrder.Items.Should().BeOfType(typeof(IReadOnlyList<OrderItem>)); // Error in comparison
            newOrder.Items.Should().BeOfType<IReadOnlyList<OrderItem>>(); // Error in comparison
        }
    }

    public class OrderItem
    {
        public string Partid { get; set; }
    }

    public class Order
    {
        public string OrderId { get; set; }

        private List<OrderItem> OrderItems { get; set; }

        public Order()
        {
            OrderItems = new List<OrderItem>();
        }

        public IReadOnlyList<OrderItem> Items
        {
            get
            {
                return OrderItems;
            }
        }
    }
}

I get the following error when running the unit test
Test Name: IReadOnlyListTest
Test FullName: IReadOnlyList.UnitTest1.IReadOnlyListTest
Test Source: c:\Users\Alan\Documents\Visual Studio 2013\Projects\FluentAssertions\IReadOnlyList\IReadOnlyList\UnitTest1.cs : line 12
Test Outcome: Failed
Test Duration: 0:00:00.032261

Result Message: Expected type to be System.Collections.Generic.IReadOnlyList1[[IReadOnlyList.OrderItem, IReadOnlyList, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], but found System.Collections.Generic.List1[[IReadOnlyList.OrderItem, IReadOnlyList, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].
Result StackTrace:
at FluentAssertions.Execution.LateBoundTestFramework.Throw(String message) in z:\Builds\work\b7ceef20fdfcf0ee\Shared\Execution\LateBoundTestFramework.cs:line 22
at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message) in z:\Builds\work\b7ceef20fdfcf0ee\FluentAssertions.Net40\Execution\TestFrameworkProvider.cs:line 42
at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message) in z:\Builds\work\b7ceef20fdfcf0ee\FluentAssertions.Core\Execution\DefaultAssertionStrategy.cs:line 25
at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args) in z:\Builds\work\b7ceef20fdfcf0ee\FluentAssertions.Core\Execution\AssertionScope.cs:line 197
at FluentAssertions.Types.TypeAssertions.Be(Type expected, String because, Object[] reasonArgs) in z:\Builds\work\b7ceef20fdfcf0ee\FluentAssertions.Core\Types\TypeAssertions.cs:line 53
at FluentAssertions.Primitives.ReferenceTypeAssertions`2.BeOfType(Type expectedType, String because, Object[] reasonArgs) in z:\Builds\work\b7ceef20fdfcf0ee\FluentAssertions.Core\Primitives\ReferenceTypeAssertions.cs:line 143
at IReadOnlyList.UnitTest1.IReadOnlyListTest() in c:\Users\Alan\Documents\Visual Studio 2013\Projects\FluentAssertions\IReadOnlyList\IReadOnlyList\UnitTest1.cs:line 16

I'm expecting System.Collections.Generic.IReadOnlyList1 back from newOrder.Items but FluentAssertions seems to be picking up System.Collections.Generic.List1

MS Test
Assert.IsInstanceOfType(newOrder.Items, typeof(IReadOnlyList));
seems to pick it up correctly, but FA
newOrder.Items.Should().BeOfType(typeof(IReadOnlyList));
seems to pick up the underlying private type of List rather than the exposed public IReadOnlyList

I'm just wondering if I have the syntax for Fluent Assertions wrong or if there is a bug in FA.

Thanks

RouteDataAssertions is not compatible with the latest version of FluentAssertions

Hi,

My project uses nuget package 0.5.0.0 of FluentAssertions.MVC5. I've just upgraded to v3.3.0 of FluentAssertions and I notice there are some breaking changes in Fluent Assertions.

When running a test that uses the RouteDataAssertions:

routeData.Should()
.HaveController("Customer")
.HaveAction("Edit")
.HaveValue("Id", 123);

I receive the following MissingMethodException:
System.MissingMethodException: Method not found: 'Boolean FluentAssertions.Execution.AssertionScope.FailWith(System.String, System.Object[])'.

Looks like Fluent Assertions has changed the signature to this method to be:

public Continuation FailWith(string message, params object[] args)

(ref: dennisdoomen/fluentassertions@2cf13d4)
Rik

[DISCUSSION] Version and package naming for AspNet Core MVC 1 support

I'd like to get some feedback on naming and version strategy for AspNet Core MVC 1 support.

Historically we have had separate packages for each version of MVC we support. The reasons for this are:

  1. We didn't support testing all features of MVC so it didn't make sense to call it a 1.0 product.
  2. Some users had difficulty with binding redirects and constraining package versions.

Moving forward I am thinking about maintaining one FluentAssertions.AspNetCore.Mvc package and resetting it's version number to 1.0. It's major version would be pegged to the Microsoft.AspNetCore.Mvc package, so that when they introduce new / breaking changes at v2 FluentAssertions.AspNetCore.Mvc would also increase to v2.

See: aspnet/Home#1255

Thoughts?

Setup AppVeyor CI

@CaseyBurns I'm trying to setup AppVeyor, but it looks like you beat me to it.

It looks like your AppVeyor project is building branches, but mine is not. I've opened a ticket with AppVeyor to find out why. I suspect there is a problem with the web hooks.

Are you able to bump up my permissions in this repro so I can check the web hooks (I'm not even sure that is an option for collaborators)?

Also, how do you feel about giving me permissions to your AppVeyor project? I think there will be some config changes needed to get the new MVC 6 projects building.

My goal here is to get to one click deployment to NuGet. I have this running for my other projects already. As you can image, I feel uneasy about sharing my NuGet API key. If you like the idea of running the CI from your AppVeyor account (I think it makes sense, but will need a little more initial setup on your part to give me permissions), I think creating a new FluentAssertions.MVC NuGet user with it's own API key, and making it a package owner would be a good solution. I'm happy to sort all the NuGet stuff out.

Thoughts?

RedirectToRoute assert reports failure, however it is not.

Installed MVC4 version of FluentAssertions.MVC (via nuget).
Solution has set .Net Framework 4.5.1 and MVC 5 Web.
Trying to use it in tests resulted in this weird error. I suspect there is some incompatibility issues somewhere...

Test Source:    ***\BaseControllerTests.cs : line 31
Test Outcome:   Failed
Test Duration:  0:00:00.1784214

Result Message: Expected ActionResult to be "RedirectToRouteResult", but found "RedirectToRouteResult"
Result StackTrace:  
at FluentAssertions.Execution.LateBoundTestFramework.Throw(String message) in d:\Workspace\Github\FluentAssertions\FluentAssertions.Net35\Execution\LateBoundTestFramework.cs:line 17
   at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message) in d:\Workspace\Github\FluentAssertions\FluentAssertions.Net35\Execution\DefaultAssertionStrategy.cs:line 23
   at FluentAssertions.Execution.AssertionScope.FailWith(String failureMessage, Object[] failureArgs) in d:\Workspace\Github\FluentAssertions\FluentAssertions.Net35\Execution\AssertionScope.cs:line 165
   at FluentAssertions.Mvc.ActionResultAssertions.BeRedirectToRouteResult(String reason, Object[] reasonArgs)

Test code:

            // prepare
            var context = HttpMocks.GetControllerContextMock().Object;
            var contr = new UnprotectedBase { ControllerContext = context };

            // act
            var result = contr.RedirectBack();

            // assert
            result.Should().BeRedirectToRouteResult();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.