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

时间:2015-01-12 21:41:36

标签: css ruby-on-rails ruby ruby-on-rails-3 rspec

我想测试我的css文件和javascript文件(本地)低于500kb(我想确保在我们开发的任何时候,这些文件严重影响我们的前端仍然低于某个值)。< / p>

我使用以下代码但收到错误

/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。

1 个答案:

答案 0 :(得分:1)

对于运营商比较,您需要使用be

expect(35).to be >= 20

查看the documentation了解更多示例。