|
1 | 1 | require File.expand_path('../../../../test_helper', __FILE__)
|
2 |
| -require 'generators/jsonapi/resource_generator' |
| 2 | +require 'generators/jsonapi/resource/resource_generator' |
3 | 3 |
|
4 | 4 | module Jsonapi
|
5 | 5 | class ResourceGeneratorTest < Rails::Generators::TestCase
|
6 | 6 | tests ResourceGenerator
|
7 |
| - destination Rails.root.join('../resources') |
| 7 | + destination File.expand_path('../tmp', __dir__ ) |
8 | 8 | setup :prepare_destination
|
9 | 9 | teardown :cleanup_destination_root
|
10 | 10 |
|
11 | 11 | def cleanup_destination_root
|
12 | 12 | FileUtils.rm_rf destination_root
|
13 | 13 | end
|
14 | 14 |
|
| 15 | + def prepare_destination |
| 16 | + super |
| 17 | + FileUtils.cp_r File.expand_path('app_template/', __dir__ ) + '/.', destination_root |
| 18 | + end |
| 19 | + |
15 | 20 | test "resource is created" do
|
16 |
| - run_generator ["post"] |
| 21 | + run_generator %w[post] |
| 22 | + assert_file 'app/resources/post_resource.rb', /class PostResource < JSONAPI::Resource/ |
| 23 | + end |
| 24 | + |
| 25 | + test "resource created with controller and processors" do |
| 26 | + run_generator %w[post --controller --processor] |
17 | 27 | assert_file 'app/resources/post_resource.rb', /class PostResource < JSONAPI::Resource/
|
| 28 | + assert_file 'app/controllers/posts_controller.rb', /class PostsController < JSONAPI::ResourceController/ |
| 29 | + assert_file 'app/processors/post_processor.rb', /class PostProcessor < JSONAPI::Processor/ |
| 30 | + end |
| 31 | + |
| 32 | + test "base resource class is settable" do |
| 33 | + run_generator %w[post --base_resource BaseResource] |
| 34 | + assert_file 'app/resources/post_resource.rb', /class PostResource < BaseResource/ |
18 | 35 | end
|
19 | 36 |
|
20 | 37 | test "resource is singular" do
|
21 |
| - run_generator ["posts"] |
| 38 | + run_generator %w[posts] |
22 | 39 | assert_file 'app/resources/post_resource.rb', /class PostResource < JSONAPI::Resource/
|
23 | 40 | end
|
24 | 41 |
|
25 |
| - test "resource is created with namespace" do |
26 |
| - run_generator ["api/v1/post"] |
27 |
| - assert_file 'app/resources/api/v1/post_resource.rb', /class Api::V1::PostResource < JSONAPI::Resource/ |
| 42 | + test "namespaced resource is created" do |
| 43 | + run_generator %w[api/post] |
| 44 | + assert_file 'app/resources/api/post_resource.rb', /class Api::PostResource < JSONAPI::Resource/ |
| 45 | + end |
| 46 | + |
| 47 | + test "namespaced resource, controller and processor are created" do |
| 48 | + run_generator %w[api/post --controller --processor] |
| 49 | + assert_file 'app/resources/api/post_resource.rb', /class Api::PostResource < JSONAPI::Resource/ |
| 50 | + assert_file 'app/controllers/api/posts_controller.rb', /class Api::PostsController < JSONAPI::ResourceController/ |
| 51 | + assert_file 'app/processors/api/post_processor.rb', /class Api::PostProcessor < JSONAPI::Processor/ |
| 52 | + assert_file 'config/routes.rb', /^( *)namespace :api do\n( *)jsonapi_resources :posts\n( *)end$/ |
28 | 53 | end
|
29 | 54 | end
|
30 | 55 | end
|
0 commit comments