Routing rspec with nested routes

Problem

You would like to add a routing rspec test in your rails application that tests for a nested route.
So while you have something like the following in your config/routes.rb file:

namespace :admin do
  resources :users do
    resources :audits, only: [:index]
  end
end

you want to add the following test for your route:

expect(get: '/admin/users/:user_id/audits').to route_to(controller: 'admin/audits', action: 'index')

but you are getting an error like the following:

The recognized options <{"controller"=>"admin/audits", "action"=>"index", "user_id"=>":user_id"}> did not match <{"controller"=>"admin/audits", "action"=>"index"}>, difference:.
       --- expected
       +++ actual
       @@ -1 +1 @@
       -{"controller"=>"admin/audits", "action"=>"index"}
       +{"controller"=>"admin/audits", "action"=>"index", "user_id"=>":user_id"}

Solution

You will need to change your rspec to the following using any number for the user_id:

expect(get: '/admin/users/42/audits').to route_to(controller: 'admin/audits', action: 'index', user_id: '42')

Solution adapted from here

minitest assert_routing with method included in path

Problem

When trying to use the minitest assert_routing with the first parameter representing the path as a hash that includes both the path and the method, and run the tests rails complains about SyntaxErrors.

When trying to use it as suggested in the ‘Rails 4 Test Prescriptions’ Pragmatic Programmers book (p. 172 – Minitest and Routing) which is:

assert_routing({ path: "/projects", method: "post" }, 
controller: "projects", action: "create")

the error is:

SyntaxError: 
/.../test/controllers/projects_controller_test.rb:12: 
syntax error, unexpected ',', expecting ')'
... '/projects', method: 'post' },  controller: 'projects', act...

even when trying to have the second parameter as a hash:

assert_routing({ path: "/projects", method: "post" }, 
{ controller: "projects", action: "create" })

the error is similar:

SyntaxError: 
/.../test/controllers/projects_controller_test.rb:12:
 syntax error, unexpected ',', expecting ')'
... '/projects', method: 'post' }, 
{ controller: 'projects', ac...

Solution

Seems that you need to pass the parameters enclosed in brackets, so the following would work:

assert_routing  ({ path: '/projects', method: 'post' }), 
({ controller: 'projects', action: 'create' })

Book Review: BDD in Action by John Ferguson Smart (Manning)

As the subtitle of the book ‘Behavior-driven development for the whole software lifecycle’ suggests, this is a book describing Behaviour Driven Development for the different phases of software development.

It starts with a very general description of BDD, the problems that it addresses, the general principles and the origins of it, as well as the pros and cons in different organisations and team scenarios. Throughout this general description there are plenty of references and links for anyone wishing to have more details.

This is followed by a real life project, that goes through the phases of requirement analysis, creation of the features, stories and examples and the distinction and differences between each of them. There are various techniques described, including Feature Injection for identifying business goals and supporting features, Impact Mapping for high-level requirement visualisation, Purpose-Based Alignment Model for judging how much effort you should put into different features, and the identification of stakeholders and their roles. There is also a description about Real Options and Deliberate Discovery principles.

There are more detailed examples to automating scenario steps, described in different languages (Java, Python, .NET, Javascript) and diffent tools (JBehave, Cucumber-JVM, Behave, SpecFlow and Cucumber-JS). These are used to go from executable specifications to automated acceptance tests. There two separate chapters following that, describing the two different ways for automating the acceptance criteria depending on whether they are for the UI layer or the non-UI requirements.

Last but not least, there is a chapter dedicated to explaining the relationship between BDD, TDD and Unit Testing, another one describing the idea of living documentation, as well as reporting and project management, and finally the role of BDD in continuous integration (CI) and continuous delivery.

Throughout the text there are a lot of diagrams to help explain the ideas better, as well as a lot of references for more detail. Even though most of the examples are based in Java, the principles, ideas and techniques are easily applied to other languages and tools.

So, in summary this is a very useful book for anyone wanting to learn how Behaviour Driven Development can be used in practise. There are different sections that are targeted to different roles from business stakeholders, to testers and to developers. Hopefully when your whole team reads this book, the whole idea of BDD can be understood better, and used to build the software right as well as build the right software.

Disclaimer: This book was reviewd under the Manning reviewers program.