spec/services/rapidfire/attempt_builder_spec.rb in rapidfire-4.0.0 vs spec/services/rapidfire/attempt_builder_spec.rb in rapidfire-5.0.0
- old
+ new
@@ -1,7 +1,11 @@
require 'spec_helper'
+def fixture_file_upload(path, mime_type)
+ Rack::Test::UploadedFile.new(Pathname.new(file_fixture_path).join(path), mime_type, false)
+end
+
describe Rapidfire::AttemptBuilder do
let(:survey) { FactoryGirl.create(:survey) }
let(:question1) { FactoryGirl.create(:q_short, survey: survey) }
let(:question2) { FactoryGirl.create(:q_long, survey: survey,
validation_rules: { presence: "1" }) }
@@ -26,15 +30,10 @@
params = { params: answer_params }.merge(survey: survey)
described_class.new(params)
end
let(:save_answers) { builder.save }
- before do
- [question1, question2]
- save_answers
- end
-
context "when all the answers are valid" do
let(:answer_params) do
{
question1.id.to_s => { :answer_text => "short answer" },
question2.id.to_s => { :answer_text => "long answer!" }
@@ -44,10 +43,11 @@
it "returns true" do
expect(save_answers).to be_truthy
end
it "successfully saves answers" do
+ save_answers
builder.answers.each do |answer|
expect(answer).to be_persisted
expect(question_ids).to include(answer.question_id)
end
end
@@ -64,24 +64,72 @@
it "returns false" do
expect(save_answers).to be_falsey
end
it "fails to save those answers" do
+ save_answers
expect(Rapidfire::Answer.count).to eq(0)
end
- context "when requested to save without validations" do
- let(:save_answers) { builder.save(:validate => false) }
+ if Rails::VERSION::MAJOR < 5
+ context "when requested to save without validations" do
+ let(:save_answers) { builder.save(:validate => false) }
- it "returns true" do
+ it "returns true" do
+ expect(save_answers).to be_truthy
+ end
+
+ it "saves all the answers" do
+ save_answers
+ builder.answers.each do |answer|
+ expect(answer).to be_persisted
+ expect(question_ids).to include(answer.question_id)
+ end
+ end
+ end
+ end
+ end
+
+ if "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" >= "5.2"
+ context "with a single file upload question" do
+ let(:question3) { FactoryGirl.create(:q_file, survey: survey) }
+
+ let(:answer_params) do
+ {
+ question1.id.to_s => { :answer_text => "short answer" },
+ question2.id.to_s => { :answer_text => "long answer" },
+ question3.id.to_s => { :file => fixture_file_upload("one.txt", "text/plain") }
+ }
+ end
+
+ it "saves the file successfully" do
expect(save_answers).to be_truthy
+
+ answer = Rapidfire::Answer.last
+ expect(answer).to be_persisted
+ expect(answer.file.download).to eq("one\n")
end
+ end
- it "saves all the answers" do
- builder.answers.each do |answer|
- expect(answer).to be_persisted
- expect(question_ids).to include(answer.question_id)
- end
+ context "with multiple files upload question" do
+ let(:question3) { FactoryGirl.create(:q_multifile, survey: survey) }
+
+ let(:answer_params) do
+ {
+ question1.id.to_s => { :answer_text => "short answer" },
+ question2.id.to_s => { :answer_text => "long answer" },
+ question3.id.to_s => { :files => [fixture_file_upload("one.txt", "text/plain"),
+ fixture_file_upload("two.txt", "text/plain")]}
+ }
+ end
+
+ it "saves the file successfully" do
+ expect(save_answers).to be_truthy
+
+ answer = Rapidfire::Answer.last
+ expect(answer).to be_persisted
+ expect(answer.files[0].download).to eq("one\n")
+ expect(answer.files[1].download).to eq("two\n")
end
end
end
end
end