2012-03-21 29 views
3

我有以下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_idnil)。

該控制器具有此方法:

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引用相同的數據庫條目,但不是同一個對象)。因此,我在檢查stripe_customer_id是否爲零之前嘗試致電@user.reload,但測試仍然失敗。

2問題:我是否應該在請求規範中檢查模型屬性?而且,有沒有辦法讓我的失敗測試通過?

感謝

+0

您是否嘗試過爲該規範加載新的用戶對象? @ user2 = User.find_by_id(@ user.id) – 2012-03-29 07:59:20

回答

1

我看你使用WebKit的驅動程序,所以這個問題可能是在交易中設置。你有沒有關掉config.use_transactional_fixturesspec_helper?另請參閱Capybara Readme(事務和數據庫設置)