require 'rails_helper' require 'shared_account_spec' RSpec.describe "课程", :type => :request do let(:course){FactoryGirl.attributes_for(:course)} describe "创建课程" do include_context "create user" before { shared_register shared_update_user } context "参数正确,成功创建课程" do before{ # uu = current_user # allow(User).to receive(:current).and_return(uu) # allow(uu).to receive(:allowed_to?).and_return(123) post courses_path, "course[name]"=> course[:name], "class_period"=> course[:class_period], "time"=> course[:time], "term"=> course[:term], "course[password]"=>course[:password], "course[description]"=> course[:description], "course[is_public]"=> course[:is_public], "course[open_student]"=> course[:open_student] } it{expect(assigns(:course)).to be_instance_of(Course)} it{expect(response).to redirect_to(settings_course_url(assigns(:course), course_type: 1))} end end describe "配置课程" do let (:course) {FactoryGirl.create(:course)} 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 end end end