Ruby TDD and Rspec
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
add a comment |
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
Hint: If you want to extract something from an array usemap
noteach
. For example:sentence.split.map { |w| w[0] }.join
does it all.
– tadman
Nov 22 at 17:48
add a comment |
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
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
ruby rspec tdd
edited Nov 22 at 17:45
Aleksei Matiushkin
78.7k95190
78.7k95190
asked Nov 22 at 17:40
b.herring
848
848
Hint: If you want to extract something from an array usemap
noteach
. For example:sentence.split.map { |w| w[0] }.join
does it all.
– tadman
Nov 22 at 17:48
add a comment |
Hint: If you want to extract something from an array usemap
noteach
. For example:sentence.split.map { |w| w[0] }.join
does it all.
– tadman
Nov 22 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 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 at 17:48
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 22 at 17:58
NMerkl
414
414
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 22 at 17:59
Todd A. Jacobs
55.5k1190157
55.5k1190157
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Hint: If you want to extract something from an array use
map
noteach
. For example:sentence.split.map { |w| w[0] }.join
does it all.– tadman
Nov 22 at 17:48