failure with creating an instance in specs

By : anti-k

I have a model Ticket which has department_id, and Department with

enum name: { dept1: 0, dept2: 1, dept3: 2 }

I have seeded db with these three departments

Department.create(name: :dept1)
Department.create(name: :dept2)
Department.create(name: :dept3)

So I try to write specs on Ticket method

 def dept

here is an example

 describe '.dept' do   

   let!(:ticket){ create :ticket, department_id: }

   it 'should return right dept' do 
     expect(ticket.dept).to eq 'Dept1'

And I recieve an error

   Validation failed: Department can't be blank

I'm a new guy to rails, so please i9f you don't mind explain me how to write such specs( with seeded db). Any advises would be very useful for me. Thanks!

By : anti-k


You'll want to refrain from seeding your database and instead create records that you need for each test.

describe '#dept' do
  let(:department) { create :department, title: 'dept1' }
  let(:ticket) { build :ticket, department: department }

  it 'should return right dept' do 
    expect(ticket.dept).to eq 'Dept1'

Notice that I also changed ticket so it's generated by build instead of create. Based on what I see, it doesn't look like you need the overhead of persisting ticket to the database in order to run this particular test.

Also, another small point... But the "convention" (if such a thing exists) is to describe instance methods with hashes in front of them instead of a dot. (Dot denotes a class method.)

Instead of using jQuery's prevUntil() function, you may simply use prev() for about the number of times you want to. A quick example would look like this:

var el = $('.start');
for (var i = 0; i < 5; i++) {
  el = el.prev();

By : Aer0

I would use the following:

var startIndex = $('.start').index();
var endIndex = startIndex - 5;

for(var i = startIndex - 1; i >= endIndex; i--){

Updated to not add class 'good' the start index.

This video can help you solving your question :)
By: admin