woshidan's loose leaf

ぼんやり勉強しています

2020-01-01から1年間の記事一覧

配列の要素全てが該当する条件を記述するallマッチャ

scopeみたいにフィルタリングした要素について、フィルタリング対象の要素について let(:included) { Item.new(price: 250) } let(:not_included) { Item.new(price: 300) } let(:array) { [included, not_included] } subject(array.filtering) it '250円以…

Railsのルーティングの細かいところについて

最近細かく引っかかっていたので。 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) 特定のクラスのインスタンス全てにあるメソッドの中身を…

ActiveRecordで関連しているモデルに対してメソッドを定義する

Userと1対多関連の関係にあるCommentというモデルがあるとする。 あるUserに関連したコメントに対して user.commentes.some_method のようにスコープやメソッドを定義したいという場合があって、その場合以下のように書ける。 class User < ApplicationRecor…

attributes_forでテスト用のハッシュを取得するときはFactoryに定義された属性しか取り出されない

下準備としてモデルの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…