Answer the question
In order to leave comments, you need to log in
How do factories work in rspec tests?
Hello, please help me understand the logic of model tests using factories.
Here is the test itself:
require 'rails_helper'
RSpec.describe News do
news1 = FactoryGirl.create(:news)
news2 = FactoryGirl.create(:news)
news1.title.should == "1 test news title"
news2.title.should == "2 test news title"
end
FactoryGirl.define do
factory :news do
sequence(:title) { |i| "#{i} test news title"}
sequence(:description) { |i| "#{i} test news description, must have minimum 40 letters"}
user_id 1
news_poster { fixture_file_upload "#{Rails.root}/spec/fixtures/images/calendar.png", 'image/png' }
end
end
class News < ActiveRecord::Base
belongs_to :user
has_attached_file :news_poster, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :news_poster, content_type: /\Aimage\/.*\Z/
validates :title, presence: true, uniqueness: true, length: { minimum: 5, maximum: 40 }
validates :description, presence: true, length: { minimum: 40, maximum: 250 }
validates :news_poster, presence: true
end
Answer the question
In order to leave comments, you need to log in
Sequences are reset after each run.
Use the database_cleaner gem
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question