2020-01-01から1年間の記事一覧
scopeみたいにフィルタリングした要素について、フィルタリング対象の要素について let(:included) { Item.new(price: 250) } let(:not_included) { Item.new(price: 300) } let(:array) { [included, not_included] } subject(array.filtering) it '250円以…
最近細かく引っかかっていたので。 URLヘルパのnew, editの修飾子は前に付く xxxx_new_pathかnew_xxxx_pathかよく迷っていたけど、 new_xxxx_path, edit_xxxx_path のほうが正解。namespaceで # routes.rb namespace 'admin' do resources xxxxs end となっ…
特定のオブジェクトにあるメソッドの中身を実行せず結果だけ返して欲しい場合 allowとreceiveを使ってスタブの指定をします。 allow(some_object).to receive(:method_name).and_return(return_value) 特定のクラスのインスタンス全てにあるメソッドの中身を…
Userと1対多関連の関係にあるCommentというモデルがあるとする。 あるUserに関連したコメントに対して user.commentes.some_method のようにスコープやメソッドを定義したいという場合があって、その場合以下のように書ける。 class User < ApplicationRecor…
下準備としてモデルのSchemeとFactoryの定義 # schema.rb ActiveRecord::Schema.define(version: 2020_11_23_064724) do create_table "users", force: :cascade do |t| t.string "name" t.integer "age" t.string "mail" t.datetime "created_at", precisio…