新手问题 RSpec 的 controller spec

ThanksSirAlec · 2018年01月18日 · 最后由 Awlter1 回复于 2018年01月18日 · 1295 次阅读

我的路由资源是这么定义的

resources :example1, only: %i(index), shallow: true do
          resources :example2, module: 'example1', only: %i(create update destroy show index)
        end

example2 放在了 example1 的文件夹下面。 然后运行测试的时候得到了

No route matches {:action=>"index", :controller=>"example1/example2"}

No route matches {:action=>"show", :controller=>"example1/example2"}

的错误

一下的 spec 的代码

RSpec.describe Example1::Example2Controller, type: :controller do
  describe "GET index" do
    it "has a 200 status code" do
      get :index
      expect(response.status).to eq(200)
    end
  end
  describe "GET show" do
    it "has a 200 status code" do
      get :show
      expect(response.status).to eq(200)
    end
  end
end
$ rake routes | grep example
example1_example2_index GET  /example1/:example1_id/example2(.:format) example1/example2#index
                 POST /example1/:example1_id/example2(.:format) example1/example2#create
example2 GET  /example2/:id(.:format)                   example1/example2#show
                 PATCH  /example2/:id(.:format)                   example1/example2#update
                 PUT    /example2/:id(.:format)                   example1/example2#update
                 DELETE /example2/:id(.:format)                   example1/example2#destroy
example1_index GET    /example1(.:format)                       example1#index

https://stackoverflow.com/questions/37399068/rspec-controller-for-nested-resources-before-action

测试 show action 时 应该没有 example1, hmmm 我再查查

需要 登录 后方可回复, 如果你还没有账号请 注册新账号