简体   繁体   中英

How to test file size on rspec (Rails 4/rspec3)

I would like to test that my css file and javascript file (locally) are under 500kb (I want to make sure that at any time of our developments, these files impacting heavily our front end remain below a certain value).

I am using the following code but am getting an error

/spec/controllers/application_controller_spec.rb

describe 'Sizes of files that significantly impact app frontend are under a certain size' do
    it "should have css files under 500ko " do
      expect( File.size("app/assets/stylesheets/custom.css.scss") ).to be <= 500
    end 

    it "should have javascript filec" do
      expect( File.size("app/assets/stylesheets/custom.css.scss") ).to be <= 500
    end 
  end

but I am getting error:

ApplicationController Sizes of files that significantly impact app frontend are under a certain size should have css files under 500ko 
 Failure/Error: expect( File.size("app/assets/stylesheets/custom.css.scss") ).to be <= 500
   expected: <= 500
        got:    15296
 # ./spec/controllers/application_controller_spec.rb:90:in `block (3 levels) in <top (required)>'
 # /home/mathieu/.rvm/gems/ruby-2.0.0-p451@rails3tutorial2ndEd/gems/zeus-0.15.3/lib/zeus/rails.rb:212:in `test'
 # /home/mathieu/.rvm/gems/ruby-2.0.0-p451@rails3tutorial2ndEd/gems/zeus-0.15.3/lib/zeus.rb:148:in `block in command'
 # /home/mathieu/.rvm/gems/ruby-2.0.0-p451@rails3tutorial2ndEd/gems/zeus-0.15.3/lib/zeus.rb:135:in `fork'
 # /home/mathieu/.rvm/gems/ruby-2.0.0-p451@rails3tutorial2ndEd/gems/zeus-0.15.3/lib/zeus.rb:135:in `command'
 # /home/mathieu/.rvm/gems/ruby-2.0.0-p451@rails3tutorial2ndEd/gems/zeus-0.15.3/lib/zeus.rb:50:in `go'
 # -e:1:in `<main>'

How come I get this number? is there some kind of multiplication I should do to convert kb in bits or something?

EDIT

Indeed I needed to convert 500Kb in bytes.

For operator comparisons you need to use be :

expect(35).to be >= 20

Check the documentation for more examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM