前提・実現したいこと
現在rails tutorialを進めている初学者の者です。
8章においてrails test(rails tutorial8章リスト8.28)が通らず詰まったので質問させて頂きます。
分かるかたいらっしゃいましたら教えて頂きたいです。
拙い文章でわかりにくいとは思いますがどうぞよろしくお願いいたします。
##以下エラーメッセージです。
errorとfailureが一つづつでています。
エラーメッセージ $ rails test Running via Spring preloader in process 5965 Started with run options --seed 61929 FAIL["test_email_validation_should_reject_invalid_addresses", UserTest, 0.4517165880001812] test_email_validation_should_reject_invalid_addresses#UserTest (0.45s) "foo@bar..com" should be invalid test/models/user_test.rb:40:in `block (2 levels) in <class:UserTest>' test/models/user_test.rb:38:in `each' test/models/user_test.rb:38:in `block in <class:UserTest>' ERROR["test_invalid_signup_information", UsersSignupTest, 0.4625528070000655] test_invalid_signup_information#UsersSignupTest (0.46s) ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"users"}, missing required keys: [:id] test/integration/users_signup_test.rb:8:in `block (2 levels) in <class:UsersSignupTest>' test/integration/users_signup_test.rb:7:in `block in <class:UsersSignupTest>' 22/22: [=========================================] 100% Time: 00:00:00, Time: 00:00:00 Finished in 0.52020s
以下必要になりそうなファイルを載せていきます。
//user_signup_test.rb require 'test_helper' class UsersSignupTest < ActionDispatch::IntegrationTest test "invalid signup information" do get signup_path assert_no_difference 'User.count' do post user_path, params: { user: { name: "", email: "user@invalid", password: "foo", password_confirmation: "bar" } } end assert_template 'users/new' assert_select 'div#error_explanation' assert_select 'div.alert' end test "valid signup information" do get signup_path assert_difference 'User.count', 1 do post users_path, params: { user: { name: "Example User", email: "user@example.com", password: "password", password_confirmation: "password" } } end follow_redirect! assert_template 'users/show' assert is_logged_in? end end
//user_test.rb require 'test_helper' class UserTest < ActiveSupport::TestCase def setup @user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar") end test "should be valid" do assert @user.valid? end test "name should be present" do @user.name = " " assert_not @user.valid? end test "email should be present" do @user.email = " " assert_not @user.valid? end test "name should not be too long" do @user.name = "a" * 51 assert_not @user.valid? end test "email should not be too long" do @user.email = "a" * 244 + "@example.com" assert_not @user.valid? end test "email validation should reject invalid addresses" do invalid_addresses = %w[user@example,com user_at_foo.org user.name@example. foo@bar_baz.com foo@bar+baz.com foo@bar..com] invalid_addresses.each do |invalid_address| @user.email = invalid_address assert_not @user.valid?, "#{invalid_address.inspect} should be invalid" end end test "email addresses should be unique" do duplicate_user = @user.dup duplicate_user.email = @user.email.upcase @user.save assert_not duplicate_user.valid? end test "email addresses should be saved as lower-case" do mixed_case_email = "Foo@ExAMPle.CoM" @user.email = mixed_case_email @user.save assert_equal mixed_case_email.downcase, @user.reload.email end test "password should be present (nonblank)" do @user.password = @user.password_confirmation = " " * 6 assert_not @user.valid? end test "password should have a minimum length" do @user.password = @user.password_confirmation = "a" * 5 assert_not @user.valid? end end
//user_controller.rb class UsersController < ApplicationController def show @user = User.find(params[:id]) end def new @user = User.new end def create @user = User.new(user_params) if @user.save log_in @user flash[:success] = "Welcome to the Sample App!" redirect_to @user else render 'new' end end private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end end
//routes.rb Rails.application.routes.draw do root 'static_pages#home' get '/help', to: 'static_pages#help' get '/about', to: 'static_pages#about' get '/contact', to: 'static_pages#contact' get '/signup', to: 'users#new' get '/login', to: 'sessions#new' post '/login', to: 'sessions#create' delete '/logout', to: 'sessions#destroy' resources :users end
##users.yml michael: name: Michael Example email: michael@example.com password_digest: <%= User.digest('password') %>
##users_login_test.rb require 'test_helper' class UsersLoginTest < ActionDispatch::IntegrationTest def setup @user = users(:michael) end test "login with invalid information" do get login_path assert_template 'sessions/new' post login_path, params: { session: { email: "", password: "" } } assert_template 'sessions/new' assert_not flash.empty? get root_path assert flash.empty? end test "login with valid information" do get login_path post login_path, params: { session: { email: @user.email, password: 'password' } } assert_redirected_to @user follow_redirect! assert_template 'users/show' assert_select "a[href=?]", login_path, count: 0 assert_select "a[href=?]", logout_path assert_select "a[href=?]", user_path(@user) end end
試したこと
エラーメッセージにて"foo@bar..com"がinvalidと出ているので試しにuser_test_rbファイルの
test "email validation should reject invalid addresses"内にある"foo@bar..com"を消した所一応は通りましたが"foo@bar..com"もinvalidとして設定したいので何処を直したらいいか教えて頂きたいです。
もう一つのerrorに関しては何処かに引数を入れなければならないのかと考えてはいるのですがそれが何処なのか、またはその考え自体が間違っているのかわからない状況です。
補足情報(FW/ツールのバージョンなど)
macbookair
catalinaOS
AWS cloud9にて実行中
rails -v 5.1.6
rails tutorial 5
回答1件
あなたの回答
tips
プレビュー