Ruby TDD and Rspec












1















Im new to testing in ruby with Rspec. I'm just wanting to write a simple test to see if the below code works. Im not sure how to do it. The code returns an acronym of a given string. thanks



def acronym(sentence)
first_letters =
sentence.split.each do |word|
first_letters << word[0]
end
first_letters.join
end

describe "acro method" do
it "returns acronym of words" do

end
end









share|improve this question

























  • Hint: If you want to extract something from an array use map not each. For example: sentence.split.map { |w| w[0] }.join does it all.

    – tadman
    Nov 22 '18 at 17:48
















1















Im new to testing in ruby with Rspec. I'm just wanting to write a simple test to see if the below code works. Im not sure how to do it. The code returns an acronym of a given string. thanks



def acronym(sentence)
first_letters =
sentence.split.each do |word|
first_letters << word[0]
end
first_letters.join
end

describe "acro method" do
it "returns acronym of words" do

end
end









share|improve this question

























  • Hint: If you want to extract something from an array use map not each. For example: sentence.split.map { |w| w[0] }.join does it all.

    – tadman
    Nov 22 '18 at 17:48














1












1








1








Im new to testing in ruby with Rspec. I'm just wanting to write a simple test to see if the below code works. Im not sure how to do it. The code returns an acronym of a given string. thanks



def acronym(sentence)
first_letters =
sentence.split.each do |word|
first_letters << word[0]
end
first_letters.join
end

describe "acro method" do
it "returns acronym of words" do

end
end









share|improve this question
















Im new to testing in ruby with Rspec. I'm just wanting to write a simple test to see if the below code works. Im not sure how to do it. The code returns an acronym of a given string. thanks



def acronym(sentence)
first_letters =
sentence.split.each do |word|
first_letters << word[0]
end
first_letters.join
end

describe "acro method" do
it "returns acronym of words" do

end
end






ruby rspec tdd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 17:45









Aleksei Matiushkin

83k95693




83k95693










asked Nov 22 '18 at 17:40









b.herringb.herring

989




989













  • Hint: If you want to extract something from an array use map not each. For example: sentence.split.map { |w| w[0] }.join does it all.

    – tadman
    Nov 22 '18 at 17:48



















  • Hint: If you want to extract something from an array use map not each. For example: sentence.split.map { |w| w[0] }.join does it all.

    – tadman
    Nov 22 '18 at 17:48

















Hint: If you want to extract something from an array use map not each. For example: sentence.split.map { |w| w[0] }.join does it all.

– tadman
Nov 22 '18 at 17:48





Hint: If you want to extract something from an array use map not each. For example: sentence.split.map { |w| w[0] }.join does it all.

– tadman
Nov 22 '18 at 17:48












2 Answers
2






active

oldest

votes


















0














Use RSpec matchers to check that what your method outputs actually matches what you expect it to do.
https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers



describe "acro method" do
it "returns acronym of words" do
test_sentence = "this is a test acronym"
expected_acronym = "tiata"

expect(acronym(test_sentence)).to eq(expected_acronym)
end
end





share|improve this answer































    1














    Define Your Input and Expected Output



    The point of TDD is to test expected behavior. To construct a test, you must define both your fixture (a known input value) and your expectation (the output you expect your method to produce given a known input value). You then compare the results of your spec with a suitable matcher. For example:



    def acronym(sentence)
    first_letters =
    sentence.split.each do |word|
    first_letters << word[0]
    end
    first_letters.join
    end

    describe "#acronym" do
    let(:sentence) { 'A very short sentence.' }

    it "returns initial letter of each word" do
    expect(acronym sentence).to eq('Avss')
    end
    end


    When you run the spec in document format, it should read fairly naturally.




    $ rspec --format doc foo_spec.rb 

    #acronym
    returns initial letter of each word

    Finished in 0.0017 seconds (files took 0.12358 seconds to load)
    1 example, 0 failures



    If you change your test's expected output from Avss to avss, then your expectation will fail. A well-written test will give you a useful error like:




    Failures:

    1) #acronym returns initial letter of each word
    Failure/Error: expect(acronym sentence).to eq('avss')

    expected: "avss"
    got: "Avss"

    (compared using ==)



    You can then fix your class or method until the desired behavior is achieved.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435995%2fruby-tdd-and-rspec%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Use RSpec matchers to check that what your method outputs actually matches what you expect it to do.
      https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers



      describe "acro method" do
      it "returns acronym of words" do
      test_sentence = "this is a test acronym"
      expected_acronym = "tiata"

      expect(acronym(test_sentence)).to eq(expected_acronym)
      end
      end





      share|improve this answer




























        0














        Use RSpec matchers to check that what your method outputs actually matches what you expect it to do.
        https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers



        describe "acro method" do
        it "returns acronym of words" do
        test_sentence = "this is a test acronym"
        expected_acronym = "tiata"

        expect(acronym(test_sentence)).to eq(expected_acronym)
        end
        end





        share|improve this answer


























          0












          0








          0







          Use RSpec matchers to check that what your method outputs actually matches what you expect it to do.
          https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers



          describe "acro method" do
          it "returns acronym of words" do
          test_sentence = "this is a test acronym"
          expected_acronym = "tiata"

          expect(acronym(test_sentence)).to eq(expected_acronym)
          end
          end





          share|improve this answer













          Use RSpec matchers to check that what your method outputs actually matches what you expect it to do.
          https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers



          describe "acro method" do
          it "returns acronym of words" do
          test_sentence = "this is a test acronym"
          expected_acronym = "tiata"

          expect(acronym(test_sentence)).to eq(expected_acronym)
          end
          end






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 17:58









          NMerklNMerkl

          414




          414

























              1














              Define Your Input and Expected Output



              The point of TDD is to test expected behavior. To construct a test, you must define both your fixture (a known input value) and your expectation (the output you expect your method to produce given a known input value). You then compare the results of your spec with a suitable matcher. For example:



              def acronym(sentence)
              first_letters =
              sentence.split.each do |word|
              first_letters << word[0]
              end
              first_letters.join
              end

              describe "#acronym" do
              let(:sentence) { 'A very short sentence.' }

              it "returns initial letter of each word" do
              expect(acronym sentence).to eq('Avss')
              end
              end


              When you run the spec in document format, it should read fairly naturally.




              $ rspec --format doc foo_spec.rb 

              #acronym
              returns initial letter of each word

              Finished in 0.0017 seconds (files took 0.12358 seconds to load)
              1 example, 0 failures



              If you change your test's expected output from Avss to avss, then your expectation will fail. A well-written test will give you a useful error like:




              Failures:

              1) #acronym returns initial letter of each word
              Failure/Error: expect(acronym sentence).to eq('avss')

              expected: "avss"
              got: "Avss"

              (compared using ==)



              You can then fix your class or method until the desired behavior is achieved.






              share|improve this answer




























                1














                Define Your Input and Expected Output



                The point of TDD is to test expected behavior. To construct a test, you must define both your fixture (a known input value) and your expectation (the output you expect your method to produce given a known input value). You then compare the results of your spec with a suitable matcher. For example:



                def acronym(sentence)
                first_letters =
                sentence.split.each do |word|
                first_letters << word[0]
                end
                first_letters.join
                end

                describe "#acronym" do
                let(:sentence) { 'A very short sentence.' }

                it "returns initial letter of each word" do
                expect(acronym sentence).to eq('Avss')
                end
                end


                When you run the spec in document format, it should read fairly naturally.




                $ rspec --format doc foo_spec.rb 

                #acronym
                returns initial letter of each word

                Finished in 0.0017 seconds (files took 0.12358 seconds to load)
                1 example, 0 failures



                If you change your test's expected output from Avss to avss, then your expectation will fail. A well-written test will give you a useful error like:




                Failures:

                1) #acronym returns initial letter of each word
                Failure/Error: expect(acronym sentence).to eq('avss')

                expected: "avss"
                got: "Avss"

                (compared using ==)



                You can then fix your class or method until the desired behavior is achieved.






                share|improve this answer


























                  1












                  1








                  1







                  Define Your Input and Expected Output



                  The point of TDD is to test expected behavior. To construct a test, you must define both your fixture (a known input value) and your expectation (the output you expect your method to produce given a known input value). You then compare the results of your spec with a suitable matcher. For example:



                  def acronym(sentence)
                  first_letters =
                  sentence.split.each do |word|
                  first_letters << word[0]
                  end
                  first_letters.join
                  end

                  describe "#acronym" do
                  let(:sentence) { 'A very short sentence.' }

                  it "returns initial letter of each word" do
                  expect(acronym sentence).to eq('Avss')
                  end
                  end


                  When you run the spec in document format, it should read fairly naturally.




                  $ rspec --format doc foo_spec.rb 

                  #acronym
                  returns initial letter of each word

                  Finished in 0.0017 seconds (files took 0.12358 seconds to load)
                  1 example, 0 failures



                  If you change your test's expected output from Avss to avss, then your expectation will fail. A well-written test will give you a useful error like:




                  Failures:

                  1) #acronym returns initial letter of each word
                  Failure/Error: expect(acronym sentence).to eq('avss')

                  expected: "avss"
                  got: "Avss"

                  (compared using ==)



                  You can then fix your class or method until the desired behavior is achieved.






                  share|improve this answer













                  Define Your Input and Expected Output



                  The point of TDD is to test expected behavior. To construct a test, you must define both your fixture (a known input value) and your expectation (the output you expect your method to produce given a known input value). You then compare the results of your spec with a suitable matcher. For example:



                  def acronym(sentence)
                  first_letters =
                  sentence.split.each do |word|
                  first_letters << word[0]
                  end
                  first_letters.join
                  end

                  describe "#acronym" do
                  let(:sentence) { 'A very short sentence.' }

                  it "returns initial letter of each word" do
                  expect(acronym sentence).to eq('Avss')
                  end
                  end


                  When you run the spec in document format, it should read fairly naturally.




                  $ rspec --format doc foo_spec.rb 

                  #acronym
                  returns initial letter of each word

                  Finished in 0.0017 seconds (files took 0.12358 seconds to load)
                  1 example, 0 failures



                  If you change your test's expected output from Avss to avss, then your expectation will fail. A well-written test will give you a useful error like:




                  Failures:

                  1) #acronym returns initial letter of each word
                  Failure/Error: expect(acronym sentence).to eq('avss')

                  expected: "avss"
                  got: "Avss"

                  (compared using ==)



                  You can then fix your class or method until the desired behavior is achieved.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 17:59









                  Todd A. JacobsTodd A. Jacobs

                  56.6k1192160




                  56.6k1192160






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435995%2fruby-tdd-and-rspec%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy