How do I write a Rails 3.1 engine controller test in rspec?

后端 未结 7 2026
北荒
北荒 2020-12-02 12:37

I have written a Rails 3.1 engine with the namespace Posts. Hence, my controllers are found in app/controllers/posts/, my models in app/models/posts, etc. I can test the mod

7条回答
  •  时光说笑
    2020-12-02 13:04

    I'm developing a gem for my company that provides an API for the applications we're running. We're using Rails 3.0.9 still, with latest Rspec-Rails (2.10.1). I was having a similar issue where I had defined routes like so in my Rails engine gem.

    match '/companyname/api_name' => 'CompanyName/ApiName/ControllerName#apimethod'
    

    I was getting an error like

    ActionController::RoutingError:
     No route matches {:controller=>"company_name/api_name/controller_name", :action=>"apimethod"}
    

    It turns out I just needed to redefine my route in underscore case so that RSpec could match it.

    match '/companyname/api_name' => 'company_name/api_name/controller_name#apimethod'
    

    I guess Rspec controller tests use a reverse lookup based on underscore case, whereas Rails will setup and interpret the route if you define it in camelcase or underscore case.

提交回复
热议问题