0
\$\begingroup\$

It's an exercise from a RSpec-course, which I'm currently doing.

Task:

  • Create a compound expectation, which asserts the string sportscar starts with the substring "sports" and ends with the substring "car".

  • Create a compound expectation, which asserts that the number 30 is even and that it responds to the times method.

  • Create a compound expectation, which asserts that the array [2, 4, 8, 16, 32, 64] includes the element 32 and starts with the elements 2, 4, and 8.

My solution:

RSpec.describe "sportscar" do
    it "should start with 'sports' and end with 'car'" do
        expect(subject).to start_with("sports").and end_with("car")
    end

    it "30 is even and responds to the times-method" do
        numb = 30
        expect(numb.even?).to be true and numb.respond_to? :times
    end

    it "shall include 32 and start with the values 2, 4, 8" do
        arr = [2, 4, 8, 16, 32, 64]
        expect(arr).to include(32) and start_with(2, 4, 8)
    end
end

Could it become improved? Perhaps more written in a Ruby-Idiomatic way?

Are my message-strings (it "should start with ..." etc.) done in a good way? Or should it be written in a different way and if so: How?

\$\endgroup\$
2
  • \$\begingroup\$ Your second and third examples are not compound rspec expressions. You should either use .and (dot and) or & (single ampersand) cf relishapp.com/rspec/rspec-expectations/docs/… \$\endgroup\$ Oct 31, 2021 at 3:25
  • \$\begingroup\$ @MarcRohloff Thanks a lot for your hint and the link. \$\endgroup\$ Oct 31, 2021 at 6:27

1 Answer 1

1
\$\begingroup\$

I don't know what the exercise says exactly but your describe does not match the two last expectations. You should have 3 describes because you describe three different things (a sportscar, number 30 and an array).

RSpec.describe "sportscar" do
    it "should start with 'sports' and end with 'car'" do
        expect(subject).to start_with("sports").and end_with("car")
    end
end

RSpec.describe 30 do
    it "is even and responds to the times-method" do
        numb = 30
        expect(numb.even?).to be true and numb.respond_to? :times
    end
end

RSpec.describe Array do
    it "includes 32 and starts with the values 2, 4, 8" do
        arr = [2, 4, 8, 16, 32, 64]
        expect(arr).to include(32) and start_with(2, 4, 8)
    end
end

This sentence also does not make sense

it 30 is even and responds to the times-method

So it should be something like

it is even and responds to the times-method

I would also not test the implementation but functionality

Instead of responds_to execute the function

count = 0
30.times do { count += 1 }
expect(count).to eq(30)

Generally I would advice to split distinct functionality into it's own it block. and, or etc are usually a sign that there is more than one functionality. Not sure if this is part of the exercise though.

it "should start with 'sports'" do
  expect(subject).to start_with("sports")
end

it "should end with 'car'" do
  expect(subject).to end_with("car")
end

To be fair, all these examples don't feel very natural.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.