PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_nifty_scaffold_generator.rb

https://github.com/intinno/nifty-generators
Ruby | 533 lines | 465 code | 54 blank | 14 comment | 3 complexity | 2f21eabb97e3155381468f628b1c5357 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" do
  74. assert_generated_file "app/models/line_item.rb" do |body|
  75. assert_match "class LineItem < ActiveRecord::Base", body
  76. end
  77. end
  78. end
  79. context "generator with index action" do
  80. rails_generator :nifty_scaffold, "line_item", "index"
  81. should_generate_file "app/views/line_items/index.html.erb"
  82. should "generate controller with index action" do
  83. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  84. assert_match "def index", body
  85. assert_match "@line_items = LineItem.all", body
  86. assert_no_match(/ def index/, body)
  87. end
  88. end
  89. end
  90. context "generator with show action" do
  91. rails_generator :nifty_scaffold, "line_item", "show"
  92. should_generate_file "app/views/line_items/show.html.erb"
  93. should "generate controller with show action" do
  94. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  95. assert_match "def show", body
  96. assert_match "@line_item = LineItem.find(params[:id])", body
  97. end
  98. end
  99. end
  100. context "generator with new and create actions" do
  101. rails_generator :nifty_scaffold, "line_item", "new", "create"
  102. should_not_generate_file "app/views/line_items/create.html.erb"
  103. should_not_generate_file "app/views/line_items/_form.html.erb"
  104. should "render form in 'new' template" do
  105. assert_generated_file "app/views/line_items/new.html.erb" do |body|
  106. assert_match "<% form_for @line_item do |f| %>", body
  107. end
  108. end
  109. should "generate controller with actions" do
  110. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  111. assert_match "def new", body
  112. assert_match "@line_item = LineItem.new\n", body
  113. assert_match "def create", body
  114. assert_match "@line_item = LineItem.new(params[:line_item])", body
  115. assert_match "if @line_item.save", body
  116. assert_match "flash[:notice] = \"Successfully created line item.\"", body
  117. assert_match "redirect_to root_url", body
  118. assert_match "render :action => 'new'", body
  119. end
  120. end
  121. end
  122. context "generator with edit and update actions" do
  123. rails_generator :nifty_scaffold, "line_item", "edit", "update"
  124. should_not_generate_file "app/views/line_items/update.html.erb"
  125. should_not_generate_file "app/views/line_items/_form.html.erb"
  126. should "render form in 'edit' template" do
  127. assert_generated_file "app/views/line_items/edit.html.erb" do |body|
  128. assert_match "<% form_for @line_item do |f| %>", body
  129. end
  130. end
  131. should "generate controller with actions" do
  132. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  133. assert_match "def edit", body
  134. assert_match "@line_item = LineItem.find(params[:id])", body
  135. assert_match "def update", body
  136. assert_match "if @line_item.update_attributes(params[:line_item])", body
  137. assert_match "flash[:notice] = \"Successfully updated line item.\"", body
  138. assert_match "redirect_to root_url", body
  139. assert_match "render :action => 'edit'", body
  140. end
  141. end
  142. end
  143. context "generator with edit and update actions" do
  144. rails_generator :nifty_scaffold, "line_item", "destroy"
  145. should_not_generate_file "app/views/line_items/destroy.html.erb"
  146. should "generate controller with action" do
  147. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  148. assert_match "def destroy", body
  149. assert_match "@line_item = LineItem.find(params[:id])", body
  150. assert_match "@line_item.destroy", body
  151. assert_match "flash[:notice] = \"Successfully destroyed line item.\"", body
  152. assert_match "redirect_to root_url", body
  153. end
  154. end
  155. end
  156. context "generator with new and edit actions" do
  157. rails_generator :nifty_scaffold, "line_item", "new", "edit"
  158. should_generate_file "app/views/line_items/_form.html.erb"
  159. should "render the form partial in views" do
  160. %w[new edit].each do |action|
  161. assert_generated_file "app/views/line_items/#{action}.html.erb" do |body|
  162. assert_match "<%= render :partial => 'form' %>", body
  163. end
  164. end
  165. end
  166. end
  167. context "generator with attributes and actions" do
  168. rails_generator :nifty_scaffold, "line_item", "name:string", "new", "price:float", "index", "available:boolean"
  169. should "render a form field for each attribute in 'new' template" do
  170. assert_generated_file "app/views/line_items/new.html.erb" do |body|
  171. assert_match "<%= f.text_field :name %>", body
  172. assert_match "<%= f.text_field :price %>", body
  173. assert_match "<%= f.check_box :available %>", body
  174. end
  175. end
  176. end
  177. context "generator with show, create, and update actions" do
  178. rails_generator :nifty_scaffold, "line_item", "show", "create", "update"
  179. should "redirect to line item show page, not index" do
  180. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  181. assert_match "redirect_to @line_item", body
  182. assert_no_match(/redirect_to line_items_url/, body)
  183. end
  184. end
  185. end
  186. context "generator with attributes and skip model option" do
  187. rails_generator :nifty_scaffold, "line_item", "foo:string", :skip_model => true
  188. should "use passed attribute" do
  189. assert_generated_file "app/views/line_items/_form.html.erb" do |body|
  190. assert_match "<%= f.text_field :foo %>", body
  191. end
  192. end
  193. should "not generate migration file" do
  194. assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
  195. end
  196. should_not_generate_file "app/models/line_item.rb"
  197. end
  198. context "generator with no attributes" do
  199. rails_generator :nifty_scaffold, "line_item"
  200. should "not generate migration file" do
  201. assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
  202. end
  203. should_not_generate_file "app/models/line_item.rb"
  204. end
  205. context "generator with only new and edit actions" do
  206. rails_generator :nifty_scaffold, "line_item", "new", "edit"
  207. should "included create and update actions in controller" do
  208. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  209. assert_match "def create", body
  210. assert_match "def update", body
  211. assert_match "root_url", body
  212. end
  213. end
  214. end
  215. context "generator with exclemation mark and show, new, and edit actions" do
  216. rails_generator :nifty_scaffold, "line_item", "!", "show", "new", "edit"
  217. should "only include index and destroy actions" do
  218. assert_generated_file "app/controllers/line_items_controller.rb" do |body|
  219. %w[index destroy].each { |a| assert_match "def #{a}", body }
  220. %w[show new create edit update].each do |a|
  221. assert_no_match(/def #{a}/, body)
  222. end
  223. end
  224. end
  225. end
  226. context "generator with --skip-controller" do
  227. rails_generator :nifty_scaffold, "line_item", :skip_controller => true
  228. should_not_generate_file "app/controllers/line_items_controller.rb"
  229. should_not_generate_file "app/helpers/line_items_helper.rb"
  230. should_not_generate_file "app/views/line_items/index.html.erb"
  231. end
  232. context "generator with --skip-migration" do
  233. rails_generator :nifty_scaffold, "line_item", "name:string", :skip_migration => true
  234. should "not generate migration file" do
  235. assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
  236. end
  237. end
  238. context "generator with --skip-timestamps" do
  239. rails_generator :nifty_scaffold, "line_item", "name:string", :skip_timestamps => true
  240. should "generate migration with no timestamps" do
  241. file = Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").first
  242. assert file, "migration file doesn't exist"
  243. assert_generated_file "db/migrate/#{File.basename(file)}" do |body|
  244. assert_no_match(/t.timestamps/, body)
  245. end
  246. end
  247. end
  248. context "existing model" do
  249. setup do
  250. Dir.mkdir("#{RAILS_ROOT}/app") unless File.exists?("#{RAILS_ROOT}/app")
  251. Dir.mkdir("#{RAILS_ROOT}/app/models") unless File.exists?("#{RAILS_ROOT}/app/models")
  252. File.open("#{RAILS_ROOT}/app/models/recipe.rb", 'w') do |f|
  253. f.puts "raise 'should not be loaded'"
  254. end
  255. end
  256. teardown do
  257. FileUtils.rm_rf "#{RAILS_ROOT}/app"
  258. end
  259. context "generator with skip model option" do
  260. rails_generator :nifty_scaffold, "recipe", :skip_model => true
  261. should "use model columns for attributes" do
  262. assert_generated_file "app/views/recipes/_form.html.erb" do |body|
  263. assert_match "<%= f.text_field :foo %>", body
  264. assert_match "<%= f.text_field :bar %>", body
  265. assert_match "<%= f.text_field :book_id %>", body
  266. assert_no_match(/:id/, body)
  267. assert_no_match(/:created_at/, body)
  268. assert_no_match(/:updated_at/, body)
  269. end
  270. end
  271. should "not generate migration file" do
  272. assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
  273. end
  274. end
  275. context "generator with attribute specified" do
  276. rails_generator :nifty_scaffold, "recipe", "zippo:string"
  277. should "use specified attribute" do
  278. assert_generated_file "app/views/recipes/_form.html.erb" do |body|
  279. assert_match "<%= f.text_field :zippo %>", body
  280. end
  281. end
  282. end
  283. end
  284. context "with spec dir" do
  285. setup do
  286. Dir.mkdir("#{RAILS_ROOT}/spec") unless File.exists?("#{RAILS_ROOT}/spec")
  287. end
  288. teardown do
  289. FileUtils.rm_rf "#{RAILS_ROOT}/spec"
  290. end
  291. context "generator with some attributes" do
  292. rails_generator :nifty_scaffold, "line_item", "name:string", "description:text"
  293. should_generate_file "spec/models/line_item_spec.rb"
  294. should "have controller specs for each action" do
  295. assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
  296. assert_match "get :index", body
  297. assert_match "get :show", body
  298. assert_match "get :new", body
  299. assert_match "get :edit", body
  300. assert_match "post :create", body
  301. assert_match "put :update", body
  302. assert_match "delete :destroy", body
  303. end
  304. end
  305. should "have fixture with attributes" do
  306. assert_generated_file "spec/fixtures/line_items.yml" do |body|
  307. assert_match "name: MyString", body
  308. assert_match "description: MyText", body
  309. end
  310. end
  311. end
  312. context "generator with new and index actions" do
  313. rails_generator :nifty_scaffold, "line_item", "new", "index"
  314. should "have controller spec with only mentioned actions" do
  315. assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
  316. assert_match "get :index", body
  317. assert_match "get :new", body
  318. assert_match "post :create", body
  319. assert_no_match(/get :show/, body)
  320. assert_no_match(/get :edit/, body)
  321. assert_no_match(/put :update/, body)
  322. assert_no_match(/delete :destroy/, body)
  323. end
  324. end
  325. should "redirect to index action on successful create" do
  326. assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
  327. assert_match "redirect_to(line_items_url)", body
  328. end
  329. end
  330. end
  331. context "generator with edit and index actions" do
  332. rails_generator :nifty_scaffold, "line_item", "edit", "index"
  333. should "redirect to index action on successful update" do
  334. assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
  335. assert_match "redirect_to(line_items_url)", body
  336. end
  337. end
  338. end
  339. context "generator with testunit specified" do
  340. rails_generator :nifty_scaffold, "line_item", "name:string", :test_framework => :testunit
  341. should_generate_file "test/unit/line_item_test.rb"
  342. end
  343. end
  344. context "with test dir" do
  345. setup do
  346. Dir.mkdir("#{RAILS_ROOT}/test") unless File.exists?("#{RAILS_ROOT}/test")
  347. end
  348. teardown do
  349. FileUtils.rm_rf "#{RAILS_ROOT}/test"
  350. end
  351. context "generator with some attributes" do
  352. rails_generator :nifty_scaffold, "line_item", "name:string", "description:text"
  353. should_generate_file "test/unit/line_item_test.rb"
  354. should "have controller tests for each action" do
  355. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  356. assert_match "get :index", body
  357. assert_match "get :show", body
  358. assert_match "get :new", body
  359. assert_match "get :edit", body
  360. assert_match "post :create", body
  361. assert_match "put :update", body
  362. assert_match "delete :destroy", body
  363. end
  364. end
  365. should "have fixture with attributes" do
  366. assert_generated_file "test/fixtures/line_items.yml" do |body|
  367. assert_match "name: MyString", body
  368. assert_match "description: MyText", body
  369. end
  370. end
  371. end
  372. context "generator with new and index actions" do
  373. rails_generator :nifty_scaffold, "line_item", "new", "index"
  374. should "have controller test with only mentioned actions" do
  375. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  376. assert_match "get :index", body
  377. assert_match "get :new", body
  378. assert_match "post :create", body
  379. assert_no_match(/get :show/, body)
  380. assert_no_match(/get :edit/, body)
  381. assert_no_match(/put :update/, body)
  382. assert_no_match(/delete :destroy/, body)
  383. end
  384. end
  385. should "redirect to index action on successful create" do
  386. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  387. assert_match "assert_redirected_to line_items_url", body
  388. end
  389. end
  390. end
  391. context "generator with edit and index actions" do
  392. rails_generator :nifty_scaffold, "line_item", "edit", "index"
  393. should "redirect to index action on successful update" do
  394. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  395. assert_match "assert_redirected_to line_items_url", body
  396. end
  397. end
  398. end
  399. context "generator with rspec specified" do
  400. rails_generator :nifty_scaffold, "line_item", "name:string", :test_framework => :rspec
  401. should_generate_file "spec/models/line_item_spec.rb"
  402. end
  403. context "generator with shoulda specified" do
  404. rails_generator :nifty_scaffold, "line_item", "name:string", :test_framework => :shoulda
  405. should "have controller and model tests using shoulda syntax" do
  406. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  407. assert_match " should ", body
  408. end
  409. assert_generated_file "test/unit/line_item_test.rb" do |body|
  410. assert_match " should ", body
  411. end
  412. end
  413. end
  414. end
  415. context "generator with haml option" do
  416. rails_generator :nifty_scaffold, "LineItem", :haml => true
  417. %w[index show new edit _form].each do |action|
  418. should_generate_file "app/views/line_items/#{action}.html.haml"
  419. end
  420. should "render the form partial in views" do
  421. %w[new edit].each do |action|
  422. assert_generated_file "app/views/line_items/#{action}.html.haml" do |body|
  423. assert_match /^= render :partial => 'form'$/, body
  424. end
  425. end
  426. end
  427. end
  428. end
  429. end
  430. # just an example model we can use
  431. class Recipe < ActiveRecord::Base
  432. add_column :id, :integer
  433. add_column :foo, :string
  434. add_column :bar, :string
  435. add_column :book_id, :integer
  436. add_column :created_at, :datetime
  437. add_column :updated_at, :datetime
  438. end