PageRenderTime 37ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/test/test_nifty_scaffold_generator.rb

http://github.com/ryanb/nifty-generators
Ruby | 534 lines | 466 code | 54 blank | 14 comment | 3 complexity | 5e50b90d26735a245c215adeb0b5ed9a 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 "existing model" do
  250. setup do
  251. Dir.mkdir("#{RAILS_ROOT}/app") unless File.exists?("#{RAILS_ROOT}/app")
  252. Dir.mkdir("#{RAILS_ROOT}/app/models") unless File.exists?("#{RAILS_ROOT}/app/models")
  253. File.open("#{RAILS_ROOT}/app/models/recipe.rb", 'w') do |f|
  254. f.puts "raise 'should not be loaded'"
  255. end
  256. end
  257. teardown do
  258. FileUtils.rm_rf "#{RAILS_ROOT}/app"
  259. end
  260. context "generator with skip model option" do
  261. rails_generator :nifty_scaffold, "recipe", :skip_model => true
  262. should "use model columns for attributes" do
  263. assert_generated_file "app/views/recipes/_form.html.erb" do |body|
  264. assert_match "<%= f.text_field :foo %>", body
  265. assert_match "<%= f.text_field :bar %>", body
  266. assert_match "<%= f.text_field :book_id %>", body
  267. assert_no_match(/:id/, body)
  268. assert_no_match(/:created_at/, body)
  269. assert_no_match(/:updated_at/, body)
  270. end
  271. end
  272. should "not generate migration file" do
  273. assert Dir.glob("#{RAILS_ROOT}/db/migrate/*.rb").empty?
  274. end
  275. end
  276. context "generator with attribute specified" do
  277. rails_generator :nifty_scaffold, "recipe", "zippo:string"
  278. should "use specified attribute" do
  279. assert_generated_file "app/views/recipes/_form.html.erb" do |body|
  280. assert_match "<%= f.text_field :zippo %>", body
  281. end
  282. end
  283. end
  284. end
  285. context "with spec dir" do
  286. setup do
  287. Dir.mkdir("#{RAILS_ROOT}/spec") unless File.exists?("#{RAILS_ROOT}/spec")
  288. end
  289. teardown do
  290. FileUtils.rm_rf "#{RAILS_ROOT}/spec"
  291. end
  292. context "generator with some attributes" do
  293. rails_generator :nifty_scaffold, "line_item", "name:string", "description:text"
  294. should_generate_file "spec/models/line_item_spec.rb"
  295. should "have controller specs for each action" do
  296. assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
  297. assert_match "get :index", body
  298. assert_match "get :show", body
  299. assert_match "get :new", body
  300. assert_match "get :edit", body
  301. assert_match "post :create", body
  302. assert_match "put :update", body
  303. assert_match "delete :destroy", body
  304. end
  305. end
  306. should "have fixture with attributes" do
  307. assert_generated_file "spec/fixtures/line_items.yml" do |body|
  308. assert_match "name: MyString", body
  309. assert_match "description: MyText", body
  310. end
  311. end
  312. end
  313. context "generator with new and index actions" do
  314. rails_generator :nifty_scaffold, "line_item", "new", "index"
  315. should "have controller spec with only mentioned actions" do
  316. assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
  317. assert_match "get :index", body
  318. assert_match "get :new", body
  319. assert_match "post :create", body
  320. assert_no_match(/get :show/, body)
  321. assert_no_match(/get :edit/, body)
  322. assert_no_match(/put :update/, body)
  323. assert_no_match(/delete :destroy/, body)
  324. end
  325. end
  326. should "redirect to index action on successful create" do
  327. assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
  328. assert_match "redirect_to(line_items_url)", body
  329. end
  330. end
  331. end
  332. context "generator with edit and index actions" do
  333. rails_generator :nifty_scaffold, "line_item", "edit", "index"
  334. should "redirect to index action on successful update" do
  335. assert_generated_file "spec/controllers/line_items_controller_spec.rb" do |body|
  336. assert_match "redirect_to(line_items_url)", body
  337. end
  338. end
  339. end
  340. context "generator with testunit specified" do
  341. rails_generator :nifty_scaffold, "line_item", "name:string", :test_framework => :testunit
  342. should_generate_file "test/unit/line_item_test.rb"
  343. end
  344. end
  345. context "with test dir" do
  346. setup do
  347. Dir.mkdir("#{RAILS_ROOT}/test") unless File.exists?("#{RAILS_ROOT}/test")
  348. end
  349. teardown do
  350. FileUtils.rm_rf "#{RAILS_ROOT}/test"
  351. end
  352. context "generator with some attributes" do
  353. rails_generator :nifty_scaffold, "line_item", "name:string", "description:text"
  354. should_generate_file "test/unit/line_item_test.rb"
  355. should "have controller tests for each action" do
  356. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  357. assert_match "get :index", body
  358. assert_match "get :show", body
  359. assert_match "get :new", body
  360. assert_match "get :edit", body
  361. assert_match "post :create", body
  362. assert_match "put :update", body
  363. assert_match "delete :destroy", body
  364. end
  365. end
  366. should "have fixture with attributes" do
  367. assert_generated_file "test/fixtures/line_items.yml" do |body|
  368. assert_match "name: MyString", body
  369. assert_match "description: MyText", body
  370. end
  371. end
  372. end
  373. context "generator with new and index actions" do
  374. rails_generator :nifty_scaffold, "line_item", "new", "index"
  375. should "have controller test with only mentioned actions" do
  376. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  377. assert_match "get :index", body
  378. assert_match "get :new", body
  379. assert_match "post :create", body
  380. assert_no_match(/get :show/, body)
  381. assert_no_match(/get :edit/, body)
  382. assert_no_match(/put :update/, body)
  383. assert_no_match(/delete :destroy/, body)
  384. end
  385. end
  386. should "redirect to index action on successful create" do
  387. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  388. assert_match "assert_redirected_to line_items_url", body
  389. end
  390. end
  391. end
  392. context "generator with edit and index actions" do
  393. rails_generator :nifty_scaffold, "line_item", "edit", "index"
  394. should "redirect to index action on successful update" do
  395. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  396. assert_match "assert_redirected_to line_items_url", body
  397. end
  398. end
  399. end
  400. context "generator with rspec specified" do
  401. rails_generator :nifty_scaffold, "line_item", "name:string", :test_framework => :rspec
  402. should_generate_file "spec/models/line_item_spec.rb"
  403. end
  404. context "generator with shoulda specified" do
  405. rails_generator :nifty_scaffold, "line_item", "name:string", :test_framework => :shoulda
  406. should "have controller and model tests using shoulda syntax" do
  407. assert_generated_file "test/functional/line_items_controller_test.rb" do |body|
  408. assert_match " should ", body
  409. end
  410. assert_generated_file "test/unit/line_item_test.rb" do |body|
  411. assert_match " should ", body
  412. end
  413. end
  414. end
  415. end
  416. context "generator with haml option" do
  417. rails_generator :nifty_scaffold, "LineItem", :haml => true
  418. %w[index show new edit _form].each do |action|
  419. should_generate_file "app/views/line_items/#{action}.html.haml"
  420. end
  421. should "render the form partial in views" do
  422. %w[new edit].each do |action|
  423. assert_generated_file "app/views/line_items/#{action}.html.haml" do |body|
  424. assert_match /^= render :partial => 'form'$/, body
  425. end
  426. end
  427. end
  428. end
  429. end
  430. end
  431. # just an example model we can use
  432. class Recipe < ActiveRecord::Base
  433. add_column :id, :integer
  434. add_column :foo, :string
  435. add_column :bar, :string
  436. add_column :book_id, :integer
  437. add_column :created_at, :datetime
  438. add_column :updated_at, :datetime
  439. end