Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rspec test suite #6

Merged
merged 4 commits into from
Jan 5, 2017
Merged

Rspec test suite #6

merged 4 commits into from
Jan 5, 2017

Conversation

Bentopi
Copy link
Owner

@Bentopi Bentopi commented Jan 5, 2017

Wrote a test suite that covers the User model, user session management and nutrition calculations for users and guests.

Learned some Rspec along the way too :)

screen shot 2017-01-05 at 2 18 22 pm

@Bentopi Bentopi merged commit a5c5bab into master Jan 5, 2017
Copy link

@hassek hassek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on the file guest_macro_parser.rb there are too many numbers and letters that define a decision and is extremely hard to follow.

I would suggest to extract those values into ruby constants and maybe even a module where you define common behaviors.

Other things:

  • The age case can be extracted into a function.
  • The weight rate case should be extracted into a function and the values "Maintain", "Lose" and "Gain" should be modified to be a Constants.
  • Genders should be constants.
  • the brm should be fetched via a function.

In other words, if you could create (within the possibility) the @calories, @protein, @fat and @carbs by just calling 4 functions inside the parse! function, your code will be a lot cleaner and easier to read.

@@ -11,9 +11,9 @@ def parse!
bmr, age, tdee_coef, weight_rate_factor = 0, 0, 0, 0

if params[:gender] == "M"
bmr = ((9.99 * params[:weight].to_f) + (6.25 * params[:height].to_f) - (4.92 * age.to_f) + 5).to_i
bmr = ((9.99 * params[:weight].to_f) + (6.25 * params[:height].to_f) - (4.92 * params[:age].to_f) + 5).to_i
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does these numbers mean? could you comment on that?

Also, the bmr calculation for Male and Female are only different by 1 field (the +5 and the -161), please merge this into 1 line instead where you setup that number as a variable. i.e:

class GuestMacroParser
FEMALE = "F"
MALE = "M"
....

def get_bmr
  gender_constants = {FEMALE => -161, MALE => 5}
  gender  = gender_constants[@params[:gender]]

  weight_coef = (9.99 * params[:weight].to_f)
  height_coef = (6.25 * params[:height].to_f)
  age_coef = (4.92 * params[:age].to_f)

  return (weight + height - age + gender).to_i
end

Notice this is not tested so it's just as an example.

Now, getting the expected bmr coefficient is very easy to test, and you should do so.

@Bentopi
Copy link
Owner Author

Bentopi commented Jan 5, 2017

ok!
You're about repeating that part of the formula; I'll have to dry that up.
The coefficients are from the Mifflin St. Jeor formula.

Separating the macro parsers into a couple functions and testing them individually would probably be a better idea long term. I'll get on that ASAP.

@hassek
Copy link

hassek commented Jan 5, 2017

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants