質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Q&A

解決済

1回答

1105閲覧

rspec3で複数個判定式を繋げたい

Daimian

総合スコア53

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

1グッド

0クリップ

投稿2019/04/09 09:20

質問したいこと

下記の街灯のソースコードような状況の時、

}.to change(payment_source, :gateway_customer_profile_id).from(nil).to start_with("cus_") .and change(payment_source, :gateway_payment_profile_id).from(nil).to start_with("card_") .and change(payment_source, :cc_type).from(nil).to eq("visa")

ここの部分で複数個条件を追加して判定していますが、下記のエラーメッセージが出てしまいます。
なぜこのエラーメッセージが出るのか、原因と解決法をご教示いただけますと幸いです。w

該当のソースコード

credit_card_spec.rb

require 'spec_helper' describe Spree::Gateway::StripeGateway::CreditCard do let!(:order) { create(:order_with_line_items, state: "payment") } let!(:creditcard_payment) { create(:creditcard_payment, order: order) } let(:payment_source) { creditcard_payment.source } describe "#create_profile" do FactoryGirl.create(:creditcard_gateway) def create_profile(payment) gateway_test = Spree::Gateway::StripeGateway::CreditCard.new gateway_test.create_profile(payment) end context "stripeのvisaカードトークンを使用した時" do it "cc_typeはvisa、gateway_customer_profile_idとgateway_payment_profile_idは正常なstripeのcustomerトークンが保存されるはず" do expect { create_profile(creditcard_payment) }.to change(payment_source, :gateway_customer_profile_id).from(nil).to start_with("cus_") .and change(payment_source, :gateway_payment_profile_id).from(nil).to start_with("card_") .and change(payment_source, :cc_type).from(nil).to eq("visa") end end end end

factories.rb

FactoryGirl.define do factory :creditcard, class: Spree::CreditCard do user_id 29311 token_id { 'tok_visa' } end factory :preferences, class: Hash do secret_key { 'hogehogehogehogehoge' } publishable_key { 'hogehogehogehogehoge' } server { 'test' } test_mode true initialize_with { attributes } end factory :creditcard_gateway, class: Spree::PaymentMethod do type { 'Spree::Gateway::StripeGateway::CreditCard' } name { 'spree_credit_card' } active 1 display_on { 'both' } auto_capture 1 preferences { build(:preferences) } end factory :creditcard_payment, class: Spree::Payment do order amount { 100.0 } state { 'checkout' } association(:payment_method, factory: :creditcard_gateway) association(:source, factory: :creditcard) end end

エラーメッセージ

1) Spree::Gateway::StripeGateway::CreditCard#create_profile stripeのvisaカードトークンを使用した時 cc_typeはvisa、gateway_customer_profile_idとgateway_payment_profile_idは正常なstripeのcustomerトークンが保存されるはず Failure/Error: expect { create_profile(creditcard_payment) }.to change(payment_source, :gateway_customer_profile_id).from(nil).to start_with("cus_") .and change(payment_source, :gateway_payment_profile_id).from(nil).to start_with("card_") .and change(payment_source, :cc_type).from(nil).to eq("visa") expected `Spree::CreditCard#gateway_customer_profile_id` to have changed to start with "cus_" and change `Spree::CreditCard#gateway_payment_profile_id` from nil to start with "card_" and change `Spree::CreditCard#cc_type` from nil to eq "visa", but is now "cus_EqxhSXiZmxVyUY" # ./spec/models/spree/gateway/stripe_gateway/credit_card_spec.rb:19:in `block (4 levels) in <top (required)>' Finished in 36.66 seconds (files took 6 minutes 33 seconds to load) 1 example, 1 failure

試したこと

おそらく、andで繋げた

.and change(payment_source, :gateway_payment_profile_id).from(nil).to start_with("card_") .and change(payment_source, :cc_type).from(nil).to eq("visa")

の部分がrspecのメソッドとして判定されていないことが原因かと考えていますが、andの改行を無くしたり、

expect do create_profile(creditcard_payment) end.to change(payment_source, :gateway_customer_profile_id).from(nil).to start_with("cus_") .and change(payment_source, :gateway_payment_profile_id).from(nil).to start_with("card_") .and change(payment_source, :cc_type).from(nil).to eq("visa")

のように、 expectの後をdo ~ endに変えたりしていますが、エラーメッセージに特段の変化がありません。
恐れ入りますが、ほんとだけでも結構ですのでご教示いただけますと幸いです。

siruku6👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ruby

1.to change(省略).from(nil).to start_with("cus_") 2.and change(省略).from(nil).to start_with("card_") 3.and change(省略).from(nil).to eq("visa")

から、改行を取り除くと

ruby

1.to change(省略).from(nil).to start_with("cus_").and change(省略).from(nil).to start_with("card_").and change(省略).from(nil).to eq("visa")

となります。

問題部分のみ抽出すると

ruby

1from(nil).to start_with("cus_").and change(

.andがくっつくべき場所が間違っていますので

ruby

1from(nil).to(start_with("cus_")).and

とする必要があります。(2行目も同様にしてください)

投稿2019/04/09 12:47

asm

総合スコア15147

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Daimian

2019/04/09 15:49

ありがとうございます!正常に動きました!なぜこのようなことが分かるのでしょうか? とても助かりました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問