Tests

Нистратов Артём

anistratov@go-rpomo.ru

  • ActiveSupport::TestCase
  • rspec
  • minitest
  • cucumber
  • turnip
  • capybara

Fixtures

- snippet:
  title: test
  content: 'a + 1 = x'
  visibility: 0
- snippet:
  title: test2
  content: 'a + 2 = x'
  visibility: 1
<% 1.upto(1000) do |i| %>
snippet_<%= i %>:
  id: <%= i %>
  title: code_<%= 1 %>
  content: <%= ... %>
  visibility: <%= i < 500 ? 0 : 1  %>
<% end %>

FactoryGirl

FactoryGirl.define do
  factory :snippet do
    title {
      Faker::Lorem.sentence(3, false, 0)
    }
    content { Faker::Lorem.paragraph }
    visibility { Kernel.rand(2) }
  end
end
factory :user do
end
factory :user do
  trait :with_snippets do
  end
end
factory :user do
  trait :with_snippets do
    transient do
      snippets_count 2
      snipepts_options []
    end

    after :create do |instance, evaluator|
      options = evaluator.snippets_options
        .extract_options!

      create_list(:snippet,
        evaluator.snippets_count,
        *evaluator.snippets_options,
        options.merge(user: instance)
      )
    end
  end
end
let(:user) {
  create :user, :with_snippets,
    snippets_options: [visibility: 1]
}
let(:user) {
  create :user, :with_snippets
}
let(:user) {
  create :user, :with_snippets,
    snippets_count: 10,
    snippets_options: [visibility: 1]
}
let(:user) {
  create :user, :with_snippets,
    snippets_count: 10,
    snippets_options: [
      :with_langs,
      visibility: 1
    ]
}
describe Snippet do
end
describe Snippet do
  subject {
    FactoryGirl.create :snipept
  }

  context 'interface' do
    it { should respond_to :title }
  end
end
describe Snippet do
  subject {
    FactoryGirl.create :snipept
  }

  context 'interface' do
    it { should respond_to :title }
    it { should belongs_to :user }
  end
end
describe Snippet do
  subject {
    FactoryGirl.create :snipept
  }

  context 'interface' do
    it { should respond_to :title }
    it { should belongs_to :user }
    it { should has_many :langs }
    it { should be_valid }
  end
end

RSPEC

before :each do
  subject.langs << Langs.first
end

let(:one_language) { [Lang.first] }
let(:two_language) { [Lang.first, Lang.last] }

it 'add new langs' do
  expect { subject.langs << Lang.last }
    .to change{ subject.langs.to_a }
    .from(one_language).to(two_languages)
end
  • before :each
  • before :all
  • after :each
  • after :all
describe 'test' do
  before(:all)  { puts 'BEFORE ALL TESTS' }
  before(:each) { puts 'BEFORE EACH TEST' }

  context 'first' do
    before(:all)  { puts 'BEFORE ALL FIRST TESTS' }
    before(:each) { puts 'BEFORE EACH FIRST TEST' }
   
    2.times { it { expect(1).to eq 1 } }

    after(:each) { puts 'AFTER EACH FIRST TEST' }
    after(:all)  { puts 'AFTER ALL FIRST TESTS' }
  end

  context 'second' do
    before(:all)  { puts 'BEFORE ALL SECOND TESTS' }
    before(:each) { puts 'BEFORE EACH SECOND TEST' }

    2.times { it { expect(1).to eq 1 } }

    after(:each) { puts 'AFTER EACH SECOND TEST' }
    after(:all)  { puts 'AFTER ALL SECOND TESTS' }
  end

  after(:each) { puts 'AFTER EACH TEST' }
  after(:all)  { puts 'AFTER ALL TESTS' }
end

Preloading

  • Spork
  • Spring
  • Zeus

BONUS:

parallel_specs

http://betterspecs.org/ru

BDD

Feature: Search courses
  Courses should be searchable by topic
  Search results should provide the course code

  Scenario: Search by topic
    Given there are 240 courses which do not have the topic "biology"
    And there are 2 courses, A001 and B205, that each have "biology" as one of the topics
    When I search for "biology"
    Then I should see the following courses:
      | Course code |
      | A001        |
      | B205        |

Tests

By adone

Tests

  • 906