Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor tests + add generator tests (#241)
- Loading branch information
1 parent
f16c720
commit 335e18b
Showing
16 changed files
with
137 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ def self.table_name | |
end | ||
|
||
def self.column_names | ||
["id", "title"] | ||
%w[id title] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require "test_helper" | ||
require "generators/godmin/resource/resource_generator" | ||
|
||
module Godmin | ||
class ResourceGeneratorTest < ::Rails::Generators::TestCase | ||
tests ResourceGenerator | ||
destination File.expand_path("../../tmp", __FILE__) | ||
setup :prepare_destination | ||
|
||
def test_resource_generator_in_standalone_install | ||
system "cd #{destination_root} && rails new . --skip-test --skip-spring --skip-bundle --skip-git --quiet" | ||
system "cd #{destination_root} && bin/rails generate godmin:install --quiet" | ||
system "cd #{destination_root} && bin/rails generate godmin:resource foo bar --quiet" | ||
|
||
assert_file "config/routes.rb", /resources :foos/ | ||
assert_file "app/views/shared/_navigation.html.erb", /<%= navbar_item Foo %>/ | ||
|
||
assert_file "app/controllers/foos_controller.rb" do |content| | ||
expected_content = <<-CONTENT.strip_heredoc | ||
class FoosController < ApplicationController | ||
include Godmin::Resources::ResourceController | ||
end | ||
CONTENT | ||
assert_match expected_content, content | ||
end | ||
|
||
assert_file "app/services/foo_service.rb" do |content| | ||
expected_content = <<-CONTENT.strip_heredoc | ||
class FooService | ||
include Godmin::Resources::ResourceService | ||
attrs_for_index :bar | ||
attrs_for_show :bar | ||
attrs_for_form :bar | ||
end | ||
CONTENT | ||
assert_match expected_content, content | ||
end | ||
end | ||
|
||
def test_resource_generator_in_engine_install | ||
system "cd #{destination_root} && rails new . --skip-test --skip-spring --skip-bundle --skip-git --quiet" | ||
system "cd #{destination_root} && bin/rails plugin new fakemin --mountable --quiet" | ||
system "cd #{destination_root} && fakemin/bin/rails generate godmin:install --quiet" | ||
system "cd #{destination_root} && fakemin/bin/rails generate godmin:resource foo bar --quiet" | ||
|
||
assert_file "fakemin/config/routes.rb", /resources :foos/ | ||
assert_file "fakemin/app/views/fakemin/shared/_navigation.html.erb", /<%= navbar_item Foo %>/ | ||
|
||
assert_file "fakemin/app/controllers/fakemin/foos_controller.rb" do |content| | ||
expected_content = <<-CONTENT.strip_heredoc | ||
module Fakemin | ||
class FoosController < ApplicationController | ||
include Godmin::Resources::ResourceController | ||
end | ||
end | ||
CONTENT | ||
assert_match expected_content, content | ||
end | ||
|
||
assert_file "fakemin/app/services/fakemin/foo_service.rb" do |content| | ||
expected_content = <<-CONTENT.strip_heredoc | ||
module Fakemin | ||
class FooService | ||
include Godmin::Resources::ResourceService | ||
attrs_for_index :bar | ||
attrs_for_show :bar | ||
attrs_for_form :bar | ||
end | ||
end | ||
CONTENT | ||
assert_match expected_content, content | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require "test_helper" | ||
|
||
module Godmin | ||
module Authorization | ||
class PolicyFinderTest < ActiveSupport::TestCase | ||
class Article; end | ||
class ArticlePolicy; end | ||
|
||
def test_find_by_model | ||
klass = Class.new do | ||
extend ActiveModel::Naming | ||
|
||
def self.name | ||
"Article" | ||
end | ||
end | ||
|
||
policy = PolicyFinder.find(klass, Godmin::Authorization::PolicyFinderTest) | ||
assert_equal ArticlePolicy, policy | ||
end | ||
|
||
def test_find_by_class | ||
policy = PolicyFinder.find(Article) | ||
assert_equal ArticlePolicy, policy | ||
end | ||
|
||
def test_find_by_symbol | ||
policy = PolicyFinder.find(:article, Godmin::Authorization::PolicyFinderTest) | ||
assert_equal ArticlePolicy, policy | ||
end | ||
|
||
def test_override_policy_class_on_class | ||
klass = Class.new do | ||
def self.policy_class | ||
ArticlePolicy | ||
end | ||
end | ||
|
||
policy = PolicyFinder.find(klass) | ||
assert_equal ArticlePolicy, policy | ||
end | ||
|
||
def test_override_policy_class_on_instance | ||
klass = Class.new do | ||
def policy_class | ||
ArticlePolicy | ||
end | ||
end | ||
|
||
policy = PolicyFinder.find(klass.new) | ||
assert_equal ArticlePolicy, policy | ||
end | ||
end | ||
end | ||
end |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.