ammeter
ammeter copied to clipboard
routes specs
I tried to test the route method of generator and it doesn't work.
route "get 'my_controller#my_action'"
Maybe I missed the documentation entry but I couldn't find.
You're right there is no documentation for this because I've never tried it before :)
You can get it to work if you write a spec like:
require "spec_helper"
require 'generators/routing/routing_generator'
describe RoutingGenerator do
destination Rails.root + 'tmp/generated_files'
before do
routes = Rails.root + 'config/routes.rb'
destination = File.join(destination_root, "config")
FileUtils.mkdir_p(destination)
FileUtils.cp routes, destination
end
before { run_generator %w(posts) }
describe 'config/routes.rb' do
subject { file('config/routes.rb') }
it { should contain 'resources :posts' }
end
end
That first before block is really ugly though! Ammeter should hide that ugliness and I need to think about what the block should look like. Rails with TestUnit does this with setup :copy_routes
(https://github.com/rails/rails/blob/master/railties/test/generators/controller_generator_test.rb#L8) but I'd prefer something more general. Maybe
setup_file 'config/routes.rb'
WDYT?
I was actually making a pull request, and based the solution on the routes test from devise.
Looking closer, devise actually implements its own copy_routes, and that is what I did.
def copy_routes
routes = File.expand_path("../../dummy/config/routes.rb", __FILE__)
destination = File.join(destination_root, "config")
FileUtils.mkdir_p(destination)
FileUtils.cp routes, destination
end
describe RailsOauthProvider::Generators::InstallGenerator do
# Tell the generator where to put its output (what it thinks of as Rails.root)
destination File.expand_path("../../../tmp", __FILE__)
before do
prepare_destination
copy_routes
end
describe 'no arguments' do
#Test implementation
end
end
It is up to you to if you want to leave this as is and be a 1 to 1 representation of the Rails::Generators::TestCase, or to choose to break from the interface and enhance it.
I don't mind doing a pull request with the contents and test of my comment, just let me know what direction you want it implemented.
BTW, thanks for the gem. It looks pretty good.
Yes it would be great if you want to do a pull request.
Let's stick with copy_routes
, since this is the first time its come up and maybe its too soon to over-generalize.
Glad you find this useful. I'm looking forward to your fix.