Question

I am learning Rails with the Rails Tutorial. The author actively teaches selective bits of RSpec. In exercises after each chapter, you have the opportunity to learn to write pithier code. I have chosen to do those exercises. But, the problem is, in the following chapters, he treats the reader as if they didn't, so the pithier code you wrote lends itself to challenges when you don't know how to adapt the new 'unpithy' code he writes to it's 'pithier' counterpart.

In the pithy version, the beginning of the file lends itself to less repetition, and looks as such:

require 'spec_helper'

  describe User do

    before { @user = User.new(name: "Example User", email: "user@example.com",
                              password: "foobar", password_confirmation: "foobar") }

    subject { @user }

    it { should respond_to(:name) }
    it { should respond_to(:email) }
    it { should respond_to(:password_digest) }
    it { should respond_to(:password) }
    it { should respond_to(:password_confirmation )}

    it { should be_valid }
  .
  .
  .
end

So, then our tests look like so:

  describe "when email is not present" do
    before { @user.email = " " }
    it { should_not be_valid }
  end

When attempting to create a test for whether or not the @user.password and @user.password_confirmation are present, I am looking to continue this RSpec format.

The authors version looks as such:

describe "when email is not present" do
  before do
    @user= User.new(name: "Example User", email: "user@example.com",
                    password: " ", password_confirmation = " ")
  end
  it { should_not be_valid }
end

My attempt at it (since the author reverts to the method of doing things without the exercise of pithier RSpec code) would be as such:

describe "when password is not present" do
  before { @user.password = " ", @user.password_confirmation = " " }
  it { should_not be_valid }
end

Is this the proper way to modify multiple hash values using a before in this context?

Furthermore, I am struggling to locate answers for my RSpec questions. Does anyone have a solid routine for finding these kinds of answers?

Était-ce utile?

La solution

Usually, if you have a block that takes up multiple lines, you would use do...end instead of { }, so your code would look like:

describe "when password is not present" do
  before do
    @user.password = " "
    @user.password_confirmation = " "
  end
  it { should_not be_valid }
end

When looking for answers to rSpec related questions, I usually turn first to Google, but I would recommend trying to narrow down your search (e.g. if you are using rSpec and Capybara, check out the Capybara docs, as I have not found the rSpec docs to be very helpful). Next I would look here (as you did), so I think you're on the right track.

Autres conseils

In author version, it's before block also create an instance variable @user for entering each it description. Create multiple hash values should put to some method's params, which like this: def some_method(args, hash_options = {}). It's a ruby style to put hash params to the method, rather then put it to Rspec before block. The before block is used to instantiate required execution before entering the it block. So in your case, I would suggest not to just put multiple hash values in before block, instead, prepare some object instantiate with those hash params, so you can run the object in your it block.

I suggest to read it for have an overview of the Rspec:

Everyday Rails Testing in Rspec,
https://leanpub.com/everydayrailsrspec

Online resource:

Better Specs,
http://betterspecs.org/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top