Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. require 'rails_helper'
  2.  
  3. RSpec.describe Post, type: :model do
  4.  
  5. before(:each) do
  6. @post = FactoryBot.build(:post)
  7. end
  8.  
  9. it "has a valid factory" do
  10. expect(build(:post)).to be_valid
  11. end
  12.  
  13. context "validation" do
  14. it "is valid with valid attributes" do
  15. expect(@post).to be_a(Post)
  16. end
  17.  
  18. describe "#title" do
  19. it {expect(@post).to validate_presence_of(:title)}
  20. it {is_expected.to allow_value("This is a test for a title").for(:title)}
  21. it {is_expected.to allow_value("This is also a test for another title").for(:title)}
  22. it {is_expected.to_not allow_value("Test").for(:title)}
  23. it {is_expected.to_not allow_value("Tes" * 60).for(:title)}
  24. end
  25.  
  26. describe "#content" do
  27. it {expect(@post).to validate_presence_of(:content)}
  28. it {is_expected.to allow_value("This is some quality content. ").for(:content)}
  29. it {is_expected.to allow_value("This is some quality content. " * 100).for(:content)}
  30. it {is_expected.to_not allow_value("Nope").for(:content)}
  31. it {is_expected.to_not allow_value("This won't be ok... " * 3000).for(:content)}
  32. end
  33. end
  34.  
  35. context "public instance methods" do
  36.  
  37. describe "search" do
  38. it { expect(Post).to respond_to(:search) }
  39.  
  40. it "should return an post if search" do
  41. post = FactoryBot.create(:post, content:"Hey guys how are you")
  42. expect(Post.search('guys')).to include(post)
  43. end
  44.  
  45. it "should return all post without search" do
  46. expect(Post.search(nil)).to eq(Post.all.reverse)
  47. end
  48. end
  49.  
  50. describe "readable_date" do
  51. it { expect(@post).to respond_to(:readable_date) }
  52.  
  53. it "should a readable date" do
  54. post = FactoryBot.create(:post)
  55. expect(post.readable_date).to eq(post.created_at.strftime("%d/%m/%y à %H:%M"))
  56. end
  57. end
  58. end
  59.  
  60. context "associations" do
  61. it { expect(@post).to have_many(:comments)}
  62. it { expect(@post).to have_many(:likes)}
  63. it { expect(@post).to belong_to(:writter).class_name('User')}
  64. it { expect(@post).to belong_to(:category)}
  65. end
  66. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement