Forum: Ruby How would you write Rspec code for Enumerable#any?

249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-05-07 15:45
(Received via mailing list)
How would you write Rspec code for Enumerable#any? ?

Like : 

a = [1,2,43,54,6]
b = [6,77,11]
a.any? { |i| b.include i  } # => true
 
Regards,
Arup Rakshit
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2014-05-07 16:12
(Received via mailing list)
On Wed, May 7, 2014 at 8:44 AM, Arup Rakshit
<aruprakshit@rocketmail.com> wrote:
> How would you write Rspec code for Enumerable#any? ?
>
> Like :
>
> a = [1,2,43,54,6]
> b = [6,77,11]
> a.any? { |i| b.include i  } # => true
>
> Regards,
> Arup Rakshit

it "checks if any a are included in b" do
  a = [1,2,43,54,6]
  b = [6,77,11]
  expect(a.any?{|i| b.include i}.to be_true
end

http://rubydoc.info/gems/rspec-expectations/frames
A7fca2db6ff3f4583df98043bd5166f9?d=identicon&s=25 Panagiotis Atmatzidis (Guest)
on 2014-05-07 16:14
(Received via mailing list)
Attachment: signature.asc (832 Bytes)
Hello,

On 7 Μαϊ 2014, at 15:44 , Arup Rakshit <aruprakshit@rocketmail.com>
wrote:

> a = [1,2,43,54,6]
> b = [6,77,11]
> a.any? { |i| b.include i  } # => true


Here is a simple example:
----
> cat enum_spec.rb
require 'rspec'

RSpec.describe "Test enumerable" do
  it "returns 'true'" do
    a = [1,2,43,54,6]
    b = [6,77,11]
    expect(a.any? { |i| b.include? i}).to eq(true)
  end

  it "returns 'false'" do
    a = [1,2,43,54]
    b = [6,77,11]
    expect(a.any? { |i| b.include? i}).to eq(false)
  end
end

> rspec enum_spec.rb
..

Finished in 0.0009 seconds
2 examples, 0 failures
>

----

However, 'minitest'[1] is also very good testing framework with the
additional '+' that it comes with ruby by default. I've used both
minitest and rspec. I didn't notice any differences between the two at
core level. Rspec has a wide variety of additional gems and strong
community. Maybe more advanced users would like to point out some
differences between the two and when to use one over the other.

Regards


[1] https://github.com/seattlerb/minitest

Panagiotis (atmosx) Atmatzidis

email:  atma@convalesco.org
URL:  http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

"As you set out for Ithaca, hope the voyage is a long one, full of
adventure, full of discovery [...]" - C. P. Cavafy
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2014-05-07 16:14
(Received via mailing list)
On Wed, May 7, 2014 at 9:12 AM, tamouse pontiki
<tamouse.lists@gmail.com> wrote:
>> Arup Rakshit
>
> it "checks if any a are included in b" do
>   a = [1,2,43,54,6]
>   b = [6,77,11]
>   expect(a.any?{|i| b.include i}.to be_true

Sorry, typo:

  expect(a.any?{|i| b.include i}).to be_true

(missed closing paren)
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-05-08 07:53
(Received via mailing list)
Hi All,

Thanks for your help. It works. But I though Rspec have some inbuilt
method like _match_array_ kind of. Anyway it works.
 
Regards,
Arup Rakshit
On Wednesday, 7 May 2014 7:43 PM, tamouse pontiki
<tamouse.lists@gmail.com> wrote:

On Wed, May 7, 2014 at 9:12 AM, tamouse pontiki
<tamouse.lists@gmail.com> wrote:
>> Arup Rakshit
>
> it "checks if any a are included in b" do
>   a = [1,2,43,54,6]
>   b = [6,77,11]
>   expect(a.any?{|i| b.include i}.to be_true

Sorry, typo:

  expect(a.any?{|i| b.include i}).to be_true

(missed closing paren)
09a32175057418748822c587ac08c429?d=identicon&s=25 Abinoam Jr. (abinoampraxedes_m)
on 2014-05-09 18:16
(Received via mailing list)
Hi Arup,

From:
- http://rubydoc.info/gems/rspec-expectations
-
https://www.relishapp.com/rspec/rspec-expectations...

Collection membership
expect(actual).to include(expected)
expect(actual).to start_with(expected)
expect(actual).to end_with(expected)

Match Array
expect(actual).to match_array(expected)

Look at "contain_exactly" also...

https://www.relishapp.com/rspec/rspec-expectations...

Abinoam Jr.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.