From 1f067c62a765af503cfab36027303c079cd31b19 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Wed, 5 Apr 2017 00:35:30 +0200 Subject: [PATCH] Generate default users only once and reset db after each test --- spec/support/fixture_builder.rb | 14 ++++++++++++++ spec/support/helper_methods.rb | 4 ++-- test/dummy/app/models/entity.rb | 4 ++++ test/dummy/app/models/person.rb | 8 +++++++- 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 spec/support/fixture_builder.rb diff --git a/spec/support/fixture_builder.rb b/spec/support/fixture_builder.rb new file mode 100644 index 0000000..0e536d5 --- /dev/null +++ b/spec/support/fixture_builder.rb @@ -0,0 +1,14 @@ +# set default users as initial database for each test +RSpec.configure do |config| + config.before(:suite) do + Person.reset_database + Fabricate(:user, diaspora_id: "alice@localhost:3000") + Fabricate(:user, diaspora_id: "bob@localhost:3000") + Person.init_database = Person.database + end + + config.after(:each) do + Entity.reset_database + Person.reset_database + end +end diff --git a/spec/support/helper_methods.rb b/spec/support/helper_methods.rb index cffd839..9ac2352 100644 --- a/spec/support/helper_methods.rb +++ b/spec/support/helper_methods.rb @@ -1,10 +1,10 @@ # default users def alice - @alice ||= Fabricate(:user, diaspora_id: "alice@localhost:3000") + @alice ||= Person.find_by(diaspora_id: "alice@localhost:3000") end def bob - @bob ||= Fabricate(:user, diaspora_id: "bob@localhost:3000") + @bob ||= Person.find_by(diaspora_id: "bob@localhost:3000") end # callback expectation helper diff --git a/test/dummy/app/models/entity.rb b/test/dummy/app/models/entity.rb index b75ae0e..41135ed 100644 --- a/test/dummy/app/models/entity.rb +++ b/test/dummy/app/models/entity.rb @@ -19,5 +19,9 @@ class Entity def database @database ||= Hash.new({}) end + + def reset_database + @database = nil + end end end diff --git a/test/dummy/app/models/person.rb b/test/dummy/app/models/person.rb index 1fcfde6..cd96c6a 100644 --- a/test/dummy/app/models/person.rb +++ b/test/dummy/app/models/person.rb @@ -30,13 +30,19 @@ class Person end class << self + attr_writer :init_database + def find_by(opts) return database[:diaspora_id][opts[:diaspora_id]] if opts[:diaspora_id] database[:guid][opts[:guid]] end def database - @database ||= {diaspora_id: {}, guid: {}} + @database ||= @init_database || {diaspora_id: {}, guid: {}} + end + + def reset_database + @database = nil end end end