如何在 rspec (Rails 4/rspec3) 上测试文件大小

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

我想测试我的 css 文件和 javascript 文件(本地)是否小于 500kb(我想确保在我们开发的任何时候,这些文件都会严重影响我们的前端保持在某个值以下)。

我正在使用以下代码,但出现错误

/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

但我收到错误消息:

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>'

我怎么会得到这个号码?我应该做某种乘法来将 kb 转换为位或其他东西吗?

编辑

确实我需要以字节为单位转换 500Kb。

对于运算符比较,您需要使用 be:

expect(35).to be >= 20

查看 the documentation 以获取更多示例。