Testing Mounted Engines

When I mount an engine in config/routes.rb like so:

    mount Blog::Engine => "/blog"

I'd like to be able to test that the routing is there. I added
assert_mounts to
ActionDispatch::Assertions::RoutingAssertions (https://gist.github.com/
a57e3addfe0aa3857e12). Does anyone else see value for this in core? Is
the code okay?

Yeah i have spent the last week trying to mount an app, is anyone else
facing the same issue?

I don't think that you need to do such unit tests, without mounting an
engine you will not be able to reach engine's routes, so tests will
fail anyway.

Yeah i have spent the last week trying to mount an app

Remember that this is still an edge version and some things can (and
probably will) change. I didn't want to go with detailed guides before
finishing work on new generator.

Short story of creating mountable engine right now:

# clone rails app and install dependencies
git clone git://github.com/rails/rails.git
cd rails
bundle install

# generate engine with routes and test dummy with engine already
bundle exec ./bin/rails plugin new /path/to/engine_name --dev --
cd /path/to/engine_name
bundle install

# generate scaffold to test it
./script/rails generate scaffold post title:string content:text

# run migrations
cd test/dummy
rake engine_name:install:migrations
rake db:migrate
./script/rails s

navigate to localhost:3000/engine_name/posts

Testing is done using test/dummy.

I personally would not use that. You're just adding line to tests that
ensures that you added line to routes.

Instead of doing that I would rather add some higher level test that
checks if you can reach engine at blog.root_path. But that's just my
opinion and I'm not in core, so maybe you will get more luck with the
others :wink:

Damn, google groups ate my post, so again :wink:

Joe Fiorini:
I don't like that kind of tests. You just add a line in tests that
ensures that you added a line in routes. Better way to do that would
be to check if engine can be properly rendered inside application. But
I'm not in core and it's only my biased opinion, so maybe you will get
more luck with someone else :wink:

Remember that's still edge version, it's not in stable branch, it's
not released. While I know that things were moving slowly for engines
a few last weeks, I'll try to catch up in next few days.

Short story for creating a mountable application right now (blog as an
# close edge version of rails and install dependencies
git clone https://github.com/rails/rails.git
cd rails
bundle install

# create actual engine
bundle exec ./bin/rails plugin new /path/to/blog --dev --mountable

#install dependencies
cd /path/to/blog
bundle install

# create simple scaffold
./script/rails generate scaffold post title:string content:text

cd test/dummy
rake blog:install:migrations
rake db:migrate

./script/rails s

navigate to localhost:3000/blog/posts

I agree with Piotr.

You should never be testing that routes are there in your application, but rather that whatever’s on the end of that route is performing as it should.

Let’s say that I have a /forums route that displays a list of forums, one of them being called “The Ghetto”. In my test, I would assert that when I go to ForumsController#index that I see “The Ghetto” within “#forums h2”.

By doing this I’m testing at least three things:

  1. The route exists and that I can route to the controller

  2. The controller assigns information to a variable that the view can use

  3. The view renders that information correctly*

(Correctly is subjective, design is an inexact science)