PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/nifty-generators/test/test_nifty_scaffold_generator.rb

https://gitlab.com/x33n/Real-Estates-Software
Ruby | 538 lines | 468 code | 56 blank | 14 comment | 3 complexity | 4df41f7b730a88dff1e35c4f45c734b2 MD5 | raw file
  1. require File.join(File.dirname(__FILE__), "test_helper.rb")
  2. class TestNiftyScaffoldGenerator < Test::Unit::TestCase
  3. include NiftyGenerators::TestHelper
  4. # Some generator-related assertions:
  5. # assert_generated_file(name, &block) # block passed the file contents
  6. # assert_directory_exists(name)
  7. # assert_generated_class(name, &block)
  8. # assert_generated_module(name, &block)
  9. # assert_generated_test_for(name, &block)
  10. # The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
  11. # assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
  12. #
  13. # Other helper methods are:
  14. # app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
  15. # bare_setup - place this in setup method to create the APP_ROOT folder for each test
  16. # bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
  17. context "routed" do
  18. setup do
  19. Dir.mkdir("#{RAILS_ROOT}/config") unless File.exists?("#{RAILS_ROOT}/config")
  20. File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
  21. f.puts "ActionController::Routing::Routes.draw do |map|\n\nend"
  22. end
  23. end
  24. teardown do
  25. FileUtils.rm_rf "#{RAILS_ROOT}/config"
  26. end
  27. context "generator without name" do
  28. should "raise usage error" do
  29. assert_raise Rails::Generator::UsageError do
  30. run_rails_generator :nifty_scaffold
  31. end
  32. end
  33. end
  34. context "generator with no options and no existing model" do
  35. rails_generator :nifty_scaffold, "LineItem"
  36. should_generate_file "app/helpers/line_items_helper.rb"
  37. should "generate controller with class as camelcase name pluralized and all actions" do
  38. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  39. assert_match "class LineItemsController < ApplicationController", body
  40. %w[index show new create edit update destroy].each do |action|
  41. assert_match "def #{action}", body
  42. end
  43. end
  44. end
  45. %w[index show new edit].each do |action|
  46. should_generate_file "app/views/line_items/#{action}.html.erb"
  47. end
  48. should "have name attribute" do
  49. assert_generated_file "app/views/line_items/_form.html.erb" do |body|
  50. assert_match "<%= f.text_field :name %>", body
  51. end
  52. end
  53. should "add map.resources line to routes" do
  54. assert_generated_file "config/routes.rb" do |body|
  55. assert_match "map.resources :line_items", body
  56. end
  57. end
  58. should_not_generate_file "app/models/line_item.rb"
  59. end
  60. context "generator with some attributes" do
  61. rails_generator :nifty_scaffold, "line_item", "name:string", "description:text"
  62. should "generate migration with attribute columns" do
  63. file = Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").first
  64. assert file, "migration file doesn't exist"
  65. assert_match(/[0-9]+_create_line_items.rb$/, file)
  66. assert_generated_file "db/migrate/#{File.basename(file)}" do |body|
  67. assert_match "class CreateLineItems", body
  68. assert_match "t.string :name", body
  69. assert_match "t.text :description", body
  70. assert_match "t.timestamps", body
  71. end
  72. end
  73. should "generate model with class as camelcase name and add attr_accessible for attributes" do
  74. assert_generated_file "app/models/line_item.rb" do |body|
  75. assert_match "class LineItem < ActiveRecord::Base", body
  76. assert_match "attr_accessible :name, :description", body
  77. end
  78. end
  79. end
  80. context "generator with index action" do
  81. rails_generator :nifty_scaffold, "line_item", "index"
  82. should_generate_file "app/views/line_items/index.html.erb"
  83. should "generate controller with index action" do
  84. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  85. assert_match "def index", body
  86. assert_match "@line_items = LineItem.all", body
  87. assert_no_match(/ def index/, body)
  88. end
  89. end
  90. end
  91. context "generator with show action" do
  92. rails_generator :nifty_scaffold, "line_item", "show"
  93. should_generate_file "app/views/line_items/show.html.erb"
  94. should "generate controller with show action" do
  95. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  96. assert_match "def show", body
  97. assert_match "@line_item = LineItem.find(params[:id])", body
  98. end
  99. end
  100. end
  101. context "generator with new and create actions" do
  102. rails_generator :nifty_scaffold, "LineItem", "new", "create"
  103. should_not_generate_file "app/views/line_items/create.html.erb"
  104. should_not_generate_file "app/views/line_items/_form.html.erb"
  105. should "render form in 'new' template" do
  106. assert_generated_file "app/views/line_items/new.html.erb" do |body|
  107. assert_match "<%= form_for @line_item do |f| %>", body
  108. end
  109. end
  110. should "generate controller with actions" do
  111. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  112. assert_match "def new", body
  113. assert_match "@line_item = LineItem.new\n", body
  114. assert_match "def create", body
  115. assert_match "@line_item = LineItem.new(params[:line_item])", body
  116. assert_match "if @line_item.save", body
  117. assert_match "flash[:notice] = \"Successfully created line item.\"", body
  118. assert_match "redirect_to root_url", body
  119. assert_match "render :action => 'new'", body
  120. end
  121. end
  122. end
  123. context "generator with edit and update actions" do
  124. rails_generator :nifty_scaffold, "line_item", "edit", "update"
  125. should_not_generate_file "app/views/line_items/update.html.erb"
  126. should_not_generate_file "app/views/line_items/_form.html.erb"
  127. should "render form in 'edit' template" do
  128. assert_generated_file "app/views/line_items/edit.html.erb" do |body|
  129. assert_match "<%= form_for @line_item do |f| %>", body
  130. end
  131. end
  132. should "generate controller with actions" do
  133. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  134. assert_match "def edit", body
  135. assert_match "@line_item = LineItem.find(params[:id])", body
  136. assert_match "def update", body
  137. assert_match "if @line_item.update_attributes(params[:line_item])", body
  138. assert_match "flash[:notice] = \"Successfully updated line item.\"", body
  139. assert_match "redirect_to root_url", body
  140. assert_match "render :action => 'edit'", body
  141. end
  142. end
  143. end
  144. context "generator with edit and update actions" do
  145. rails_generator :nifty_scaffold, "line_item", "destroy"
  146. should_not_generate_file "app/views/line_items/destroy.html.erb"
  147. should "generate controller with action" do
  148. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  149. assert_match "def destroy", body
  150. assert_match "@line_item = LineItem.find(params[:id])", body
  151. assert_match "@line_item.destroy", body
  152. assert_match "flash[:notice] = \"Successfully destroyed line item.\"", body
  153. assert_match "redirect_to root_url", body
  154. end
  155. end
  156. end
  157. context "generator with new and edit actions" do
  158. rails_generator :nifty_scaffold, "line_item", "new", "edit"
  159. should_generate_file "app/views/line_items/_form.html.erb"
  160. should "render the form partial in views" do
  161. %w[new edit].each do |action|
  162. assert_generated_file "app/views/line_items/#{action}.html.erb" do |body|
  163. assert_match "<%= render :partial => 'form' %>", body
  164. end
  165. end
  166. end
  167. end
  168. context "generator with attributes and actions" do
  169. rails_generator :nifty_scaffold, "line_item", "name:string", "new", "price:float", "index", "available:boolean"
  170. should "render a form field for each attribute in 'new' template" do
  171. assert_generated_file "app/views/line_items/new.html.erb" do |body|
  172. assert_match "<%= f.text_field :name %>", body
  173. assert_match "<%= f.text_field :price %>", body
  174. assert_match "<%= f.check_box :available %>", body
  175. end
  176. end
  177. end
  178. context "generator with show, create, and update actions" do
  179. rails_generator :nifty_scaffold, "line_item", "show", "create", "update"
  180. should "redirect to line item show page, not index" do
  181. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  182. assert_match "redirect_to @line_item", body
  183. assert_no_match(/redirect_to line_items_url/, body)
  184. end
  185. end
  186. end
  187. context "generator with attributes and skip model option" do
  188. rails_generator :nifty_scaffold, "line_item", "foo:string", :skip_model => true
  189. should "use passed attribute" do
  190. assert_generated_file "app/views/line_items/_form.html.erb" do |body|
  191. assert_match "<%= f.text_field :foo %>", body
  192. end
  193. end
  194. should "not generate migration file" do
  195. assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
  196. end
  197. should_not_generate_file "app/models/line_item.rb"
  198. end
  199. context "generator with no attributes" do
  200. rails_generator :nifty_scaffold, "line_item"
  201. should "not generate migration file" do
  202. assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
  203. end
  204. should_not_generate_file "app/models/line_item.rb"
  205. end
  206. context "generator with only new and edit actions" do
  207. rails_generator :nifty_scaffold, "line_item", "new", "edit"
  208. should "included create and update actions in controller" do
  209. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  210. assert_match "def create", body
  211. assert_match "def update", body
  212. assert_match "root_url", body
  213. end
  214. end
  215. end
  216. context "generator with exclemation mark and show, new, and edit actions" do
  217. rails_generator :nifty_scaffold, "line_item", "!", "show", "new", "edit"
  218. should "only include index and destroy actions" do
  219. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  220. %w[index destroy].each { |a| assert_match "def #{a}", body }
  221. %w[show new create edit update].each do |a|
  222. assert_no_match(/def #{a}/, body)
  223. end
  224. end
  225. end
  226. end
  227. context "generator with --skip-controller" do
  228. rails_generator :nifty_scaffold, "line_item", :skip_controller => true
  229. should_not_generate_file "app/controllers/line_items_controller.rb"
  230. should_not_generate_file "app/helpers/line_items_helper.rb"
  231. should_not_generate_file "app/views/line_items/index.html.erb"
  232. end
  233. context "generator with --skip-migration" do
  234. rails_generator :nifty_scaffold, "line_item", "name:string", :skip_migration => true
  235. should "not generate migration file" do
  236. assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
  237. end
  238. end
  239. context "generator with --skip-timestamps" do
  240. rails_generator :nifty_scaffold, "line_item", "name:string", :skip_timestamps => true
  241. should "generate migration with no timestamps" do
  242. file = Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").first
  243. assert file, "migration file doesn't exist"
  244. assert_generated_file "db/migrate/#{File.basename(file)}" do |body|
  245. assert_no_match(/t.timestamps/, body)
  246. end
  247. end
  248. end
  249. context "generator with --make-fixture" do
  250. rails_generator :nifty_scaffold, "line_item", "name:string", :make_fixture => true
  251. should "have fixture with attributes" do
  252. assert_generated_file "test/fixtures/line_items.yml" do |body|
  253. assert_match "name: MyString", body
  254. end
  255. end
  256. end
  257. context "existing model" do
  258. setup do
  259. Dir.mkdir("#{RAILS_ROOT}/app") unless File.exists?("#{RAILS_ROOT}/app")
  260. Dir.mkdir("#{RAILS_ROOT}/app/models") unless File.exists?("#{RAILS_ROOT}/app/models")
  261. File.open("#{RAILS_ROOT}/app/models/recipe.rb", 'w') do |f|
  262. f.puts "raise 'should not be loaded'"
  263. end
  264. end
  265. teardown do
  266. FileUtils.rm_rf "#{RAILS_ROOT}/app"
  267. end
  268. context "generator with skip model option" do
  269. rails_generator :nifty_scaffold, "recipe", :skip_model => true
  270. should "use model columns for attributes" do
  271. assert_generated_file "app/views/recipes/_form.html.erb" do |body|
  272. assert_match "<%= f.text_field :foo %>", body
  273. assert_match "<%= f.text_field :bar %>", body
  274. assert_match "<%= f.text_field :book_id %>", body
  275. assert_no_match(/:id/, body)
  276. assert_no_match(/:created_at/, body)
  277. assert_no_match(/:updated_at/, body)
  278. end
  279. end
  280. should "not generate migration file" do
  281. assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
  282. end
  283. end
  284. context "generator with attribute specified" do
  285. rails_generator :nifty_scaffold, "recipe", "zippo:string"
  286. should "use specified attribute" do
  287. assert_generated_file "app/views/recipes/_form.html.erb" do |body|
  288. assert_match "<%= f.text_field :zippo %>", body
  289. end
  290. end
  291. end
  292. end
  293. context "with spec dir" do
  294. setup do
  295. Dir.mkdir("#{RAILS_ROOT}/spec") unless File.exists?("#{RAILS_ROOT}/spec")
  296. end
  297. teardown do
  298. FileUtils.rm_rf "#{RAILS_ROOT}/spec"
  299. end
  300. context "generator with some attributes" do
  301. rails_generator :nifty_scaffold, "line_item", "name:string", "description:text"
  302. should_generate_file "spec/models/line_item_spec.rb"
  303. should "have controller specs for each action" do
  304. assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
  305. assert_match "get :index", body
  306. assert_match "get :show", body
  307. assert_match "get :new", body
  308. assert_match "get :edit", body
  309. assert_match "post :create", body
  310. assert_match "put :update", body
  311. assert_match "delete :destroy", body
  312. end
  313. end
  314. should "generate fixture file" do
  315. assert Dir.glob("#{RAILS_ROOT}/test/fixtures/*.yml").empty?
  316. end
  317. end
  318. context "generator with new and index actions" do
  319. rails_generator :nifty_scaffold, "line_item", "new", "index"
  320. should "have controller spec with only mentioned actions" do
  321. assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
  322. assert_match "get :index", body
  323. assert_match "get :new", body
  324. assert_match "post :create", body
  325. assert_no_match(/get :show/, body)
  326. assert_no_match(/get :edit/, body)
  327. assert_no_match(/put :update/, body)
  328. assert_no_match(/delete :destroy/, body)
  329. end
  330. end
  331. should "redirect to index action on successful create" do
  332. assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
  333. assert_match "redirect_to(line_items_url)", body
  334. end
  335. end
  336. end
  337. context "generator with edit and index actions" do
  338. rails_generator :nifty_scaffold, "line_item", "edit", "index"
  339. should "redirect to index action on successful update" do
  340. assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
  341. assert_match "redirect_to(line_items_url)", body
  342. end
  343. end
  344. end
  345. context "generator with testunit specified" do
  346. rails_generator :nifty_scaffold, "line_item", "name:string", :test_framework => :testunit
  347. should_generate_file "test/unit/line_item_test.rb"
  348. end
  349. end
  350. context "with test dir" do
  351. setup do
  352. Dir.mkdir("#{RAILS_ROOT}/test") unless File.exists?("#{RAILS_ROOT}/test")
  353. end
  354. teardown do
  355. FileUtils.rm_rf "#{RAILS_ROOT}/test"
  356. end
  357. context "generator with some attributes" do
  358. rails_generator :nifty_scaffold, "line_item", "name:string", "description:text"
  359. should_generate_file "test/unit/line_item_test.rb"
  360. should "have controller tests for each action" do
  361. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  362. assert_match "get :index", body
  363. assert_match "get :show", body
  364. assert_match "get :new", body
  365. assert_match "get :edit", body
  366. assert_match "post :create", body
  367. assert_match "put :update", body
  368. assert_match "delete :destroy", body
  369. end
  370. end
  371. should "generate fixture file" do
  372. assert Dir.glob("#{RAILS_ROOT}/test/fixtures/*.yml").empty?
  373. end
  374. end
  375. context "generator with new and index actions" do
  376. rails_generator :nifty_scaffold, "line_item", "new", "index"
  377. should "have controller test with only mentioned actions" do
  378. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  379. assert_match "get :index", body
  380. assert_match "get :new", body
  381. assert_match "post :create", body
  382. assert_no_match(/get :show/, body)
  383. assert_no_match(/get :edit/, body)
  384. assert_no_match(/put :update/, body)
  385. assert_no_match(/delete :destroy/, body)
  386. end
  387. end
  388. should "redirect to index action on successful create" do
  389. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  390. assert_match "assert_redirected_to line_items_url", body
  391. end
  392. end
  393. end
  394. context "generator with edit and index actions" do
  395. rails_generator :nifty_scaffold, "line_item", "edit", "index"
  396. should "redirect to index action on successful update" do
  397. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  398. assert_match "assert_redirected_to line_items_url", body
  399. end
  400. end
  401. end
  402. context "generator with rspec specified" do
  403. rails_generator :nifty_scaffold, "line_item", "name:string", :test_framework => :rspec
  404. should_generate_file "spec/models/line_item_spec.rb"
  405. end
  406. context "generator with shoulda specified" do
  407. rails_generator :nifty_scaffold, "line_item", "name:string", :test_framework => :shoulda
  408. should "have controller and model tests using shoulda syntax" do
  409. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  410. assert_match " should ", body
  411. end
  412. assert_generated_file "test/unit/line_item_test.rb" do |body|
  413. assert_match " should ", body
  414. end
  415. end
  416. end
  417. end
  418. context "generator with haml option" do
  419. rails_generator :nifty_scaffold, "LineItem", :haml => true
  420. %w[index show new edit _form].each do |action|
  421. should_generate_file "app/views/line_items/#{action}.html.haml"
  422. end
  423. should "render the form partial in views" do
  424. %w[new edit].each do |action|
  425. assert_generated_file "app/views/line_items/#{action}.html.haml" do |body|
  426. assert_match /^= render :partial => 'form'$/, body
  427. end
  428. end
  429. end
  430. end
  431. end
  432. end
  433. # just an example model we can use
  434. class Recipe < ActiveRecord::Base
  435. add_column :id, :integer
  436. add_column :foo, :string
  437. add_column :bar, :string
  438. add_column :book_id, :integer
  439. add_column :created_at, :datetime
  440. add_column :updated_at, :datetime
  441. end