DEV Community

Learn Go by writing tests

Chris James on March 11, 2018

This post is the first in a WIP project called learn-go-with-tests. From the README Explore the Go language by writing tests Get a grounding wi...
Collapse
 
ben profile image
Ben Halpern

Wow super great post. Thanks Chris!

Collapse
 
alexmartin profile image
Alex Martin • Edited

Wo Wo Wo... Well done Chris!
This blog helps me out in solving my coding assignments on C, C++, Java, Python in my graduation level.

Cheers
Alex Martin
Content/Copy Writer
EssayCorp: essaycorp.com

Collapse
 
quii profile image
Chris James

What a lovely comment

Collapse
 
onecuteliz profile image
Elizabeth Jenerson

This was awesome. Thanks, Chris!!

Now, turning attention to the audience - if I needed a peer to help w/ creating tests:

  1. Is anyone here willing to be said peer for one hour OR
  2. Where can I go to get a better understanding?

I understand why to test, how to set up my tests (using Rspec to test RoR), but I can't wrap my brain on how to unit test logging in via Google or FB. 😟

If inappropriately posted totally delete and tell me where to go 🤪

Collapse
 
quii profile image
Chris James

I have very limited experience with Ruby.

But the principles of TDD should still be helpful

When you say

unit test logging in via Google or FB

You probably dont want to unit test this. This sounds like if anything an integration test.

You need to think about what you want to test. You dont need to test a 3rd party as you (hopefully) trust it. Even if the test failed, what could you do? You cant edit their code!

What you can test is the way you interact with the 3rd party. You would probably want to read about "mocking" to achieve this.

In short you use something called a "Spy" instead of the "real" Google/FB thing you're using. Your test runs your code and then asks the Spy "Did Liz send through params x, y & z as I expect?" That should give you enough confidence that you're calling the 3rd party correctly.

I will eventually make a post about mocking things, watch this space.

Collapse
 
onecuteliz profile image
Elizabeth Jenerson

You are correct, Chris, that I'm looking for whether the right output came through as expected... Learned that's an integration test today. 🙂

I will definitely watch for that topic.
I've briefy read how some consider mocking the devil... I'm too junior to truly understand why but it'd be great if you could speak to that point ... Maybe folk's incorrect use of it makes mocking appear horrible or speaking to cases where it may not be best to mock. 🤷🏾‍♀️

Either way, thanks immensely, your explanations were wonderful and I'll keep an eye out.

Thread Thread
 
quii profile image
Chris James

Hi Liz

Hope this helps: dev.to/quii/learn-go-by-writing-te...

Collapse
 
chancedev profile image
Chance

Just seeing this now, but really enjoyed this. As someone who is looking to pick up Go as another language, I found your style of explanation really easy to follow and frankly hammered home some concepts that were a little fuzzy to me.

I hope you'll keep writing. Thanks!

Collapse
 
dhaubert profile image
Douglas Haubert

Thanks for sharing the knowledge Chris.
I spent some hours reading godoc, that I have never heard before.

Loved the format of learning a new language such as go through TDD, because I learned testing and syntax in parallel.

Collapse
 
hgisinger profile image
Hernando Gisinger

Great post.

I think that t.ErrorF must be t.Errorf (We are calling the ErrorF method on our t...)

Thanks for sharing

Collapse
 
quii profile image
Chris James

Thanks, and good spot :)

Collapse
 
rolizon profile image
Don Elder

Unreal experience, diving is my favorite thing, now I will officially list it in one of my hobbies. redball4.us

Collapse
 
rhymes profile image
rhymes

Just read it, great post! :-)

Still a bummer there's no "default argument value" in Go, but I understand the point, especially in a strictly typed system.

You can still cheat with structs and default values though

Collapse
 
quii profile image
Chris James

Thanks!

Im not sure it being statically typed is a reason for no default values, kotlin, scala and many others have it.

I guess in Go's eyes they value explicitness over developer convenience in this case. That's not to say either is right or wrong, just an opinion on language design

Collapse
 
shajorjita profile image
RJ • Edited

I hate to sound negative, but want to caution that anyone who is planning to learn Go should take a look at this page:
github.com/ksimka/go-is-not-good

Collapse
 
northbear profile image
northbear • Edited

Some time ago I read all critic articles about Go. And now I have to say that most of them are about of matching of programming habits of authors. Most of them are pretty subjective.
By example, common place for Go critics is absence of generics. But actually some generics are not so generics as they named. They sometimes provide some requirements to object in container. Then what's difference against Go interfaces? So...
I also miss some things in Go, but I prefer to leave a place to grow. Go is pretty young.

Collapse
 
quii profile image
Chris James

I am not going to really engage with this but I had a browse of the lists and i saw

is compiled

Is a pretty interesting reason not to use a language ;)

Collapse
 
shajorjita profile image
RJ

That point definitely sounds ridiculous, however, there are genuine criticisms on that page, it's better for any newbee to skim through the list once.

Thread Thread
 
rhymes profile image
rhymes

Yeah, some of Go's criticism is definitely on point but I guess advantages and drawbacks have to be considered in each language.

I wouldn't pick Go as my first programming language :-)

Collapse
 
k_penguin_sato profile image
K-Sato • Edited

I know this is very subjective. But to me, this was one of the best posts I've read on Dev.to.
Thanks for the post, Chris!!!

Collapse
 
quii profile image
Chris James

Thanks!

Collapse
 
cumirror profile image
tongjin • Edited

great post. Thanks Chris!

and i am puzzled by the granularity of test. For example, whether we needed a test for greetingPrefix when we have had TestHello already.

Collapse
 
quii profile image
Chris James

It's interesting because Go does let you test private functions but I think it is best to avoid doing that.

The reason being is you should try to avoid testing the underlying algorithm in tests. What I mean by that is test outcomes, not the way it is done.

Why is that important?

A lot of code bases suffer when developers want to refactor, but end up having to change a load of test code so it feels like a pain.

But the very definition of refactoring should be that the behaviour of the code doesnt change! If you are testing your algorithms you are making refactoring harder.

Coming back to our example, "greetingPrefix" is just an internals of our public API. Let's pretend I did write a test, but then decided I actually preferred to not have it as a separate function (for some reason). I would have to change test code, even though I'm only refactoring.

I've waffled on a bit there, but i hope it makes sense!

Collapse
 
cumirror profile image
tongjin

It help a lot that the behaviour of the code is more importance than the way how it is done.

Usually, we reduce the internal coupling through the interface(public API), so the behavior of the interface is crucial, and the test should focus on the interface to ensure it behaves as expected. At the angle of efficiency in testing, it is not necessary to pay much attention to the internal implementation.

Thread Thread
 
hippookool profile image
hippookool

I am very interested in this topic, thanks! paperminecraft.io

Collapse
 
rolizon profile image
Don Elder

Okay, it was interesting and informative, but it would be even better if you have included real-life examples. getaway shootout

Collapse
 
rolizon profile image
Don Elder

Any luck with finding a song which you were looking for? Use Shazam mate.
dogeminer2.org

Collapse
 
emilysquotespag profile image
Emilys quotes

My boss is a cool guy, he is chill all the time and it seems like he hardly cares about anything(rocketleagueunblocked.com).

Collapse
 
rolizon profile image
Don Elder

It was horrible and disgusting, I still can’t believe how bad I performed on the stage.
idle breakout

Collapse
 
emilysquotespag profile image
Emilys quotes

Oh cool, tomorrow we will have yet another One Piece episode, can't wait till tomorrow.

Collapse
 
emilysquotespag profile image
Emilys quotes

When did you last have a deep conversation with someone? I believe it was a long time ago.

Collapse
 
rolizon profile image
Don Elder

Ahahaha yeah, it will do the job, now I see how you managed to achieve it.
Drift Hunters Unblocked

Collapse
 
emilysquotespag profile image
Emilys quotes

Stop listing your issues here(dogeminer2unblocked.com), use forum instead or open ticket….

Collapse
 
rolizon profile image
Don Elder

I have to admit, I had no idea what Coub was a couple of days ago, now I’m addicted to it and can’t stop watching it. cupcake 2048

Collapse
 
rolizon profile image
Don Elder

Don’t be afraid of modern technologies, I know things change fast, but it is totally okay and natural. Scribbleio

Collapse
 
rolizon profile image
Don Elder

So everyone else around me is getting vacation except me, is there something wrong with me? fireboyandwatergirlunblocked.co

Collapse
 
rolizon profile image
Don Elder

Flooding with SMS messages my phone from the morning... Start of the day is awful.
alchemygame.io

Collapse
 
onlinecoursesexpert profile image
markhalland

Amazing but I love to see more live coding test for learning please do some more real time based code so i can understand even better regards take my exam online for me

Collapse
 
rolizon profile image
Don Elder

Value your time, don’t waste it on arguing with stupid people.
Fly or Die