Skip to content

Instantly share code, notes, and snippets.

@sheland
Last active September 27, 2018 21:08
Show Gist options
  • Save sheland/d518bcdf749fad28b34a625fb4f71923 to your computer and use it in GitHub Desktop.
Save sheland/d518bcdf749fad28b34a625fb4f71923 to your computer and use it in GitHub Desktop.
Testing Flatten Array
require 'minitest/autorun'
require 'minitest/reporters'
require_relative 'flatten_array'
describe "flatten_array" do
it "returns a single array" do
expect(flatten_array).must_be_kind_of '['Fred', 'Paul', 'Mary']'
end
it "check for nil values" do
expect(flatten_array(['nil', '2', '14'])).must_raise ArgumentError
expect(flatten_array(['nil', 'nil', 'nil'])).must_raise ArgumentError
end
it "check for empty array" do
expect(flatten_array([''])).must_raise ArgumentError
expect(flatten_array([])).must_raise ArgumentError
end
it "check for presence of array" do
expect(flatten_array('Mary', 'Jane', 'Sally')).must_raise ArgumentError
expect(flatten_array('Mary', 'Jane', {Sara})).must_raise ArgumentError
expect(flatten_array('1 + 1').must_raise ArgumentError
end
end
@Sabineth17
Copy link

Hello Shelan,
Here is a list of my feedback about your tests:

--> You did an awesome job keeping your testing codes succinct!

--> I would suggest maybe adding some comments showing how you are arranging, acting and asserting when structuring your tests.
For instance:
describe "flatten_array" do
#Arrange
array = [['Fred'], 'Paul', ['Mary']]

#Act up on that method
new_array = flatten_array(array)

#Assert that the output is
expect(new_array).must_be_equal ['Fred', 'Paul', 'Mary']

--> Also with the above example, I believe that your 'expect' statement needs to raise the parameter being passed and not the original array. Hence, you will notice I used new_array. You might also want to change .must_be_kind to .must_be_equal. It is definitely clearer and based on our notes .must_be_kind is often followed by an enumerable rather than an object like an array. Just some food for thoughts! Make sure you check in with Chris on this.

--> Syntax: I believe you will need to add { } instead of () in your expect statements.

--> Why would the following line of code raise an argument error? Is it because of the nil values or because the arrays are not nested? Maybe follow a naming convention that is clearer and this may actually be an edge case instead of an argument error.

it "check for nil values" do
expect(flatten_array(['nil', '2', '14'])).must_raise ArgumentError
expect(flatten_array(['nil', 'nil', 'nil'])).must_raise ArgumentError
end

--> Lastly, I would write a few more tests or edge cases that are not raising Argument Errors but literally testing your code. For instance, what happens when the user enters an array that is nested or not nested, or that is nested but only contains string or integers and/or both?

--> Great idea to test an empty array! I hadn't thought about it.
Let me know if you want to talk about any of my feedback here.
Thank you for sharing,
Sabine


require 'minitest/autorun'
require 'minitest/reporters'
require_relative 'flatten_array'

describe "flatten_array" do
it "returns a single array" do
expect(flatten_array).must_be_kind_of '['Fred', 'Paul', 'Mary']'
end

it "check for nil values" do
expect(flatten_array(['nil', '2', '14'])).must_raise ArgumentError
expect(flatten_array(['nil', 'nil', 'nil'])).must_raise ArgumentError
end

it "check for empty array" do
expect(flatten_array([''])).must_raise ArgumentError
expect(flatten_array([])).must_raise ArgumentError
end

it "check for presence of array" do
expect(flatten_array('Mary', 'Jane', 'Sally')).must_raise ArgumentError
expect(flatten_array('Mary', 'Jane', {Sara})).must_raise ArgumentError
expect(flatten_array('1 + 1').must_raise ArgumentError

end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment