Home>

Please teach me about Rspec grammar.
I want to write a Rails tutorial test in Rspec, but the test doesn't pass.
I think it's a matter of how to write Rspec, but since both Rails and Rspec are beginners, I don't know how to write them correctly.

Specifically, in Chapter 6 of the Rails tutorial, Listing 6.18: Testing valid email formats, it is a test to pass email addresses with the correct pattern defined as the format according to regular expressions.

Below is the code in Minitest as per the tutorial

require'test_helper'
class UserTest</pre>
<p>And here is the code I wrote and converted to Rspec</p>
<pre><code data-language = "Rspec">require'rails_helper'
RSpec.describe User, type :: model do
  before do
    @user = User.new (name: "Example User", email: "[email protected]")
  end
  ..
  ..
  ..
  it "is acceptable with valid email" do
    valid_addresses =% w [[email protected] [email protected] [email protected] [email protected] alice + [email protected]]
    valid_addresses.each do | valid_address |
      @ user.email = valid_address
      expect (@user) .to be_valid?
    end
  end
end

And the error code is

Failures:
  1) User is acceptable with valid email
     Failure/Error: expect (@user) .to be_valid?
       expected #<User id: nil, name: "Example User", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: nil>to respond to `valid ??`
     # ./spec/models/user_spec.rb:40:in `block (3 levels) in<main>'
     # ./spec/models/user_spec.rb:38:in ʻeach'
     # ./spec/models/user_spec.rb:38:in `block (2 levels) in<main>'
     # -e: 1: in `<main>'

I think the block part is written incorrectly, but I don't know the correct notation.
I think this is a very rudimentary question, but could anyone please tell me?

  • Answer # 1

    When I googled, I found some pages of people who rewrote the Rails tutorial test with RSpec and summarized it, so I solved it by slightly changing the writing style with reference to them.
    However, I still don't understand the problems with the above writing style, so I would appreciate it if anyone could point out grammatical mistakes and teach me the correct notation.

Related articles