re: Intro to RSpec in Rails (part 1): Basic Syntax and Strategy for Testing VIEW POST

FULL DISCUSSION
 

I recommend giving BetterSpecs a read.

Where you've placed this code will cause unexpected side-effects. You'll need to wrap it into the lets function.

        rcg = RandomCharacterGenerator.new
        player = Player.create(user_name: "Ronald McDonald", display_name: "Mac")
        character = rcg.new_character("Ronnie the Rat", player)

If the code reads exactly as it does you can omit the descriptions:

    it { expect(character).to be_an_instance_of(Character) }

You'll likely want to use context.
Another resource to read

Here are adjustments (I didn't add the context since I didn't have time to think about the code):

# /spec/services/random_character_generator_spec.rb

RSpec.describe RandomCharacterGenerator do
  describe "#new_character" do
    let(:player){
      Player.create(user_name: "Ronald McDonald", display_name: "Mac")
    }
    let(:character) {
      rcg = RandomCharacterGenerator.new
      rcg.new_character("Ronnie the Rat", player)
    }

    it { expect(character).to be_an_instance_of(Character) }
  end
end
 

Thank you for this super-helpful comment Andrew! It was actually perfectly timed--I had an interview for a Rails position earlier this week, and we spent a lot of time pair programming RSpec tests, so I was REALLY THANKFUL to have you put this on my radar so quickly!! :)

I added some comments to the code and the article noting that my variable-creation is not optimal, and directing them to both this comment and the the followup article I just published covering let and context: dev.to/isalevine/intro-to-rspec-in... . I gave you a little shoutout at the bottom too, let me know if you want me to your profile/anything you've written on the subject/anything else!

Again, really appreciate your feedback and guidance! You were absolutely right, BetterSpec is DEFINITELY the resource I needed. <3

code of conduct - report abuse