|
|
|
@ -35,13 +35,53 @@ RSpec.describe "课程", :type => :request do
|
|
|
|
|
context "修改课程图片" do
|
|
|
|
|
include Rack::Test::Methods
|
|
|
|
|
let(:avatar) {Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/test.jpg",'image/jpg')}
|
|
|
|
|
|
|
|
|
|
context "正常图片上传成功" do
|
|
|
|
|
subject(:resp) {post upload_avatar_path(source_type: 'Course', source_id: course.id, format: :json),"avatar"=>{image: avatar}}
|
|
|
|
|
it{ expect(subject).to be_ok }
|
|
|
|
|
it{ expect(subject.body).not_to be_empty }
|
|
|
|
|
it "状态要为0" do
|
|
|
|
|
o = ActiveSupport::JSON.decode(subject.body)
|
|
|
|
|
expect(o["status"]).to eq(0)
|
|
|
|
|
end
|
|
|
|
|
it "要回传图片地址" do
|
|
|
|
|
o = ActiveSupport::JSON.decode(subject.body)
|
|
|
|
|
expect(o["url"]).not_to be_empty
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "不是图片,上传失败" do
|
|
|
|
|
let(:invalid_avatar) {Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/hah.txt",'text/plain')}
|
|
|
|
|
before do
|
|
|
|
|
resp = post upload_avatar_path(source_type: 'Course', source_id: course.id, format: :json),"avatar"=>{image: invalid_avatar}
|
|
|
|
|
@o = ActiveSupport::JSON.decode(resp.body)
|
|
|
|
|
end
|
|
|
|
|
it "状态要为0" do
|
|
|
|
|
expect(@o["status"]).not_to eq(0)
|
|
|
|
|
end
|
|
|
|
|
it "要回传错误信息" do
|
|
|
|
|
expect(@o["message"]).to be_include("图片")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "文件过大,上传失败" do
|
|
|
|
|
before do
|
|
|
|
|
big_file = Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/test.jpg",'image/jpg')
|
|
|
|
|
allow(ActionDispatch::Http::UploadedFile).to receive(:new).and_return(double('BigFile',size: 10*1024*1024, original_filename: 'rais.jpg', tempfile: nil))
|
|
|
|
|
# trace = TracePoint.new(:call) do |tp|
|
|
|
|
|
# p [tp.lineno, tp.defined_class, tp.method_id, tp.event] if tp.method_id == :post
|
|
|
|
|
# end
|
|
|
|
|
resp = post upload_avatar_path(source_type: 'Course', source_id: course.id, format: :json),'avatar[image]'=> big_file
|
|
|
|
|
@o = ActiveSupport::JSON.decode(resp.body)
|
|
|
|
|
end
|
|
|
|
|
it "状态要为0" do
|
|
|
|
|
expect(@o["status"]).not_to eq(0)
|
|
|
|
|
end
|
|
|
|
|
it "要回传错误信息" do
|
|
|
|
|
expect(@o["message"]).to be_include("大")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "不是图片,上传失败"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|