속성 나는 다음과 같은 RSpec에 파일이 있습니다RSpec에 요청 사양 테스트 모델은
describe "Cart" do
before do
@user = FactoryGirl.create(:user)
@cart = @user.carts.create!
end
describe "using stripe" do
before do
@sport = FactoryGirl.create(:sport)
end
describe "user adds sport to cart" do
before do
visit sports_path
click_link "Add to Cart"
end
it "should be checkout page" do
page.should have_content("Total")
end
describe "user clicks checkout" do
before do
click_button "Checkout"
end
it "should redirect user to sign in form" do
page.should have_selector('h2', text: "Sign in")
end
describe "user logs on" do
before do
fill_in "Email", with: @user.email
fill_in "Password", with: @user.password
click_button "Sign in"
end
it "should be on checkout page" do
page.should have_selector('h2', text: "Checkout")
end
describe "user fills in form", js: true, driver: :webkit do
describe "everything valid" do
before do
fill_in "card-number", with: 4242424242424242
fill_in "card-expiry-month", with: 12
fill_in "card-expiry-year", with: 2015
fill_in "card-cvc", with: 123
click_button "Submit Payment"
end
it "should redirect to confirmation page" do
page.should have_content("Confirmation")
end
it "should have the total price listed" do
page.should have_content(@cart.total_price)
end
it "should create a stripe customer and save that to the stripe_customer_id of the user" do
@user.stripe_customer_id.should_not be_nil
end
describe "should allow user authorize charge" do
before do
click_button "Confirm and Purchase"
end
it "should be back to sports page" do
page.should have_content("Select a Sport")
end
end
end
end
end
end
end
end
그래서 (FactoryGirl에 의해 생성) 사용자가 내 사이트에서 뭔가를 구입합니다.
should create a stripe customer and save that to the stripe_customer_id of the user
이 실패했습니다 (@user.stripe_customer_id
은 nil
임).
def confirmation
@cart = current_cart
customer = Stripe::Customer.create(description: current_user.email, card: params[:stripeToken])
current_user.update_attributes(stripe_customer_id: customer.id)
end
내가 CURRENT_USER (테스트 용 FactoryGirl에서 동일한 사용자)을 알고있는 다른 테스트가 작동되기 때문에 stripe_customer_id
업데이트되고 :
컨트롤러는이 메소드를 갖는다.
데이터베이스에 직접 영향을 주었기 때문에 모델을 어떻게 든 업데이트해야한다고 생각했습니다 (@user 및 current_user는 동일한 db 항목을 참조하지만 동일한 개체는 아닙니다). 그래서 stripe_customer_id
이 nil인지 확인하기 전에 @user.reload
으로 전화를 시도했지만 테스트는 여전히 실패했습니다.
2 가지 질문 : 요청 사양에서 모델 속성을 검사해야합니까? 그리고 실패 시험을 통과 할 수있는 방법이 있습니까?
감사합니다.
해당 사양에 대한 새 사용자 개체로드를 시도 했습니까? @ user2 = User.find_by_id (@ user.id) –