Vote count:
0
I'm using raw/bare-metal sql inserts to increase write performance on my service. I have something like this in my module -
insert = "('#{id}', '#{status}', '#{some_time_val}')"
sql_string = "INSERT INTO history ('device_id', 'status', 'time') VALUES #{insert}"
ActiveRecord::Base.connection.execute sql_string
When I write an rspec like below, it tests everything except whether or not the insert went through. So my expectations will never work because of the way rspec, database_cleaner etc do rollbacks and transactions. I tried using
self.use_transactional_fixtures = false
and
before(:all) do
DatabaseCleaner.strategy = nil
end
but the inserts still don't go through to my test database
describe Worker do
let (:device1) {FactoryGirl.create(:device)}
let (:device2) {FactoryGirl.create(:device)}
let (:device3) {FactoryGirl.create(:device)}
self.use_transactional_fixtures = false
before(:all) do
DatabaseCleaner.strategy = nil
end
it "does something" do
devices = [{"status" => "Offline", "time" => "2013-09-17 18:17:17", "id" => device1.id},
{"status" => "Online", "time" => "2013-09-17 18:18:18", "id" => device2.id}]
Worker.any_instance.stubs(:devices).returns(devices) ## Not important for this question
Worker.new.perform
device1.reload.status.should == "Offline" # FAILS
end
end
How would I test this? What's a good strategy to test raw sql inserts like this?
asked 55 secs ago
Rspec: How to test ActiveRecord::Base.connection.execute
Aucun commentaire:
Enregistrer un commentaire