Guest User

Untitled

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