Large rspec delimiter

-:total_cost_with_tax => #<BigDecimal:7fda9d17aaf0,'0.105225E4',18(45)>, -:total_cost_without_tax => #<BigDecimal:7fda9d17b450,'0.972E3',9(36)>, -:total_last_installment_amount => #<BigDecimal:7fda9d17b978,'0.8011E2',18(45)>, -:total_monthly_installment_amount => #<BigDecimal:7fda9d17abb8,'0.8011E2',18(45)>, -:total_tax => #<BigDecimal:7fda9d17b068,'0.8025E2',18(45)>, +:total_cost_with_tax => #<BigDecimal:7fda9d0184c8,'0.105225E4',18(36)>, +:total_cost_without_tax => #<BigDecimal:7fda91ff2b48,'0.972E3',9(27)>, +:total_last_installment_amount => #<BigDecimal:7fda91fee548,'0.8011E2',18(36)>, +:total_monthly_installment_amount => #<BigDecimal:7fda91fe72c0,'0.8011E2',18(36)>, +:total_tax => #<BigDecimal:7fda9d00a2b0,'0.8025E2',18(36)>, 

So, they are dotted on some of my tests ... rspec 2, rails 3. I compare hashes using .should eq() for comparison. I can't seem to get a spell. This seems like the exact thing that seems stupid.

+5
source share
2 answers

RSpec 3 has BigDecimal eq:

 x.should eq(y) expect(x).to eq(y) 

If you are comparing BigDecimal with Float, keep in mind that accuracy can affect the comparison.

You can use this:

 x.should be_within(delta).of(y) expect x.to be_within(delta).of(y) 

If you are comparing two BigDecimal numbers that have different prefixes, keep in mind that these numbers may show different results from inspect , as well as from hash , depending on your platform and which version of Ruby BigDecimal you are running.

For example, this could happen:

 BigDecimal.new("2").hash == BigDecimal.new("2.0").hash => false 

Your result shows that your views of BigDecimal strings are slightly different.

Here is what your lines mean:

  • Part 1 is the address of the object.
  • Part 2 is a numeric value represented as a string.
  • Part 3 is the number of significant digits, and then the maximum number of significant digits.

Your conclusion shows that your lines have different addresses of objects, the same values ​​of numbers (i.e., part 2), identical significant digits (the first number in part 3), but different numbers of maximum significant digits.

For your question, using RSpec 2 and comparing BigDecimal hashes, you will solve your problem by matching rspec be_within .

Note that the Ruby BigDecimal and Float numbers are floating point numbers:

  • A BigDecimal is a decimal floating-point number of arbitrary precision.

  • A Float is a binary precision binary number with double precision.

You can see the BigDecimal floating point decimal by doing the following:

 require 'bigdecimal' x=BigDecimal.new(100) => #<BigDecimal:7f8e62038570,'0.1E3',9(27)> 

A significant part is β€œ0.1” and the exponential part is β€œE3”.

Note that this is a typical Ruby MRI / KRI VM. Implementations may differ from other Ruby VMs, such as JRuby, because Java has its own bignum code.

When you compare two different types of floating point numbers, such as BigDecimal and Float, you may get results that may seem contradictory, because the types use different bases (decimal or binary), different prefixes and different Ruby classes.

Example:

 BigDecimal.new("1.111111111111111") === 1.111111111111111 => true BigDecimal.new("1.1111111111111111") === 1.1111111111111111 => false 
+6
source

With Rspec 3.x you can use for BigDecimal eq , as you can use it for float.

 RSpec.describe "an integer" do it "is equal to a float of the same value" do expect(5).to eq(5.0) end end 

For more information, see RSpec documentation: https://www.relishapp.com/rspec/rspec-expectations/v/3-4/docs/built-in-matchers/equality-matchers

+2
source

Source: https://habr.com/ru/post/1204294/


All Articles