Closed bploetz closed 9 years ago
Is it possible to make it working with regular controller tests? API controllers are not that different than the web controllers so it seems to be a good idea to be able to test them using the same method.
I honestly don't know why controller specs don't work with the path versioning strategy. Ultimately it's Rails under the hood that is not finding the route in the test, but it does find it when you run rake routes
or actually hit the path with the app running. Could be a Rails issue. I'll have to dig into it more and report back here.
I found another strange behaviour. When I use versionist, I'm not able to use mocks in specs. Even though I mock a method, the regular implementation is called.
class Api::V1::ItemsController < Api::V1::BaseController
def index
Tool.work(param[:data])
render json:'anything'
end
end
My spec:
describe Api::V1::ItemsController do
describe "#create" do
before(:each) do
Tool.should_receive(:work).and_return(true)
end
it "shows items" do
get :index, data:"data", format: :json
end
end
end
During debugging I tried to compare Tool
constant in the spec and in the controller and they are not equal (==
returned false). Once I removed api_version
from my routes and replaced it with namespace
it started to work correctly.
EDIT: it doesn't work without versionist as well. It's possible that mocks just doesn't work in Rails integration tests (quite reasonable approach). Still it's a good idea to enable testing vesionist controllers in normal controller specs (to enable mocks for example).
Using get :index, use_route: 'api/v1'
seems to fix the issue of Rails not finding the route (at least with the Path Strategy).
However, it raises another error:
Failure/Error: get :index, use_route: 'api/v1'
ActionView::MissingTemplate:
Missing template api/v1/base/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :arb, :jbuilder, :haml]}. Searched in:
* "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x000000092fa9f0>"
I do have a view file in app/views/api/v1/base/index.json.jbuilder.
As noted here:
http://stackoverflow.com/questions/14586573/specs-for-controller-inside-a-module/14593358#14593358
If you're using the path versioning strategy, you still need to use integration tests/request spec, but the docs only call this out for HTTP Header and Request Parameter versioning strategies. Clearly this applies to all strategies, so update the docs.