Skip to content

Instantly share code, notes, and snippets.

@hzm-s
hzm-s / show_list_spec.rb
Last active January 3, 2016 05:09
簡易的な一覧画面のレコード表示のテスト(RSpec,Rails)
before do
@resources = FactoryGirl.create_list(:resource, 10)
end
it 'それぞれのResourceが表示されていること' do
visit '/resources'
@resources.each do |r|
expect(page).to have_content(/#{r.id}\s+#{r.attr1}\s+#{r.attr2}\s+#{r.attr3}/)
end
end
@hzm-s
hzm-s / create_spec.rb
Last active January 3, 2016 04:49
異常系ValidationのE2Eテスト(RSpec,Rails)
context '入力が正しくない' do
[
[:name, "未入力", nil, "can't be blank"],
[:name, "101文字以上", 'a' * 101, "is too long"],
[:display, "101文字以上", 'a' * 101, "is too long"]
]
.each do |attr, state, value, msg|
context "#{attr.to_s.humanize}が#{state}" do
let(:input) { valid_input.merge(attr => value) }
@hzm-s
hzm-s / gist:8313204
Last active January 2, 2016 13:58
Structなクラスをハッシュでnew
class Lens < Struct.new(:name, :mm, :f, :close_up, :generation)
def initialize(attrs)
super(*self.class.members.inject([]) {|r, x| r << attrs[x] })
end
end
Lens.new(
f: 2,
generation: '3rd',
@hzm-s
hzm-s / validator_spec.rb
Created September 3, 2012 02:50
DRY'uped validator spec
require 'spec_helper'
shared_examples "valid" do
it { expect { subject }.not_to raise_error(Avalon::Error) }
end
shared_examples "invalid" do
it { expect { subject }.to raise_error(Avalon::Error) }
end