Guest User

Untitled

a guest
Feb 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. require File.dirname(__FILE__) + '/../test_helper'
  2.  
  3. class BookTest < Test::Unit::TestCase
  4. fixtures :publishers, :authors, :books, :authors_books
  5.  
  6. def test_create
  7. book = Book.new(
  8. :title => 'Ruby for Toddlers',
  9. :publisher_id => Publisher.find(1).id,
  10. :published_at => Time.now,
  11. :authors => Author.find(:all),
  12. :isbn => '123-123-123-1',
  13. :blurb => 'The best book since "Bodo Bรคr zu Hause"',
  14. :page_count => 12,
  15. :price => 40.4
  16. )
  17. assert book.save
  18. end
  19.  
  20. def test_failing_create
  21. book = Book.new
  22. assert_equal false, book.save
  23. assert_equal 7, book.errors.size
  24.  
  25. assert book.errors.on(:title)
  26. assert book.errors.on(:publisher)
  27. assert book.errors.on(:authors)
  28. assert book.errors.on(:published_at)
  29. assert book.errors.on(:isbn)
  30. assert book.errors.on(:page_count)
  31. assert book.errors.on(:price)
  32. end
  33.  
  34. def test_has_many_and_belongs_to_mapping
  35. apress = Publisher.find_by_name("Apress")
  36. assert_equal 2, apress.books.size
  37.  
  38. book = Book.new(
  39. :title => 'Rails E-Commerce 3nd Edition',
  40. :authors => [Author.find_by_first_name_and_last_name('Christian', 'Hellsten'),
  41. Author.find_by_first_name_and_last_name('Jarkko', 'Laine')],
  42. :published_at => Time.now,
  43. :isbn => '123-123-123-x',
  44. :blurb => 'E-Commerce on Rails',
  45. :page_count => 300,
  46. :price => 30.5
  47. )
  48.  
  49. apress.books << book
  50.  
  51. apress.reload
  52. book.reload
  53.  
  54. assert_equal 3, apress.books.size
  55. assert_equal 'Apress', book.publisher.name
  56. end
  57.  
  58. def test_has_and_belongs_to_many_authors_mapping
  59. book = Book.new(
  60. :title => 'Rails E-Commerce 3nd Edition',
  61. :publisher => Publisher.find_by_name('Apress'),
  62. :authors => [Author.find_by_first_name_and_last_name('Christian', 'Hellsten'),
  63. Author.find_by_first_name_and_last_name('Jarkko', 'Laine')],
  64. :published_at => Time.now,
  65. :isbn => '123-123-123-x',
  66. :blurb => 'E-Commerce on Rails',
  67. :page_count => 300,
  68. :price => 30.5
  69. )
  70.  
  71. assert book.save
  72.  
  73. book.reload
  74.  
  75. assert_equal 2, book.authors.size
  76. assert_equal 2, Author.find_by_first_name_and_last_name('Christian', 'Hellsten').books.size
  77. end
  78.  
  79. end
Add Comment
Please, Sign In to add comment