繁体   English   中英

RSpec请求规范测试模型属性

[英]RSpec Request Spec Testing Model Attribute

我有以下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个问题:我是否应该检查请求规范中的模型属性? 而且,有没有办法让失败的测试通过?

谢谢

我看到你正在使用webkit驱动程序,所以问题可能出在事务设置中。 你有没有关闭config.use_transactional_fixtures中的spec_helper 另见Capybara自述文件 (交易和数据库设置)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM