【问题标题】:Toggling working in the controller but not in the Spec: Rails 3 + Rspec切换在控制器中工作但不在规范中工作:Rails 3 + Rspec
【发布时间】:2011-05-02 12:35:48
【问题描述】:

鉴于此确认方法:

  if (@bucket.confirmation_code == @code)
      puts "OLAAA"
      @bucket.toggle!(:confirmation)
      @bucket.save
      flash[:success] = "Successfully confirmed bucket."
      redirect_to bucket_url(@bucket)
    else
      flash[:fail] = "Error confirming bucket."
      redirect_to bucket_url(@bucket)
    end

如果它切换确认属性,我有一个规范测试,但即使在控制器中,一切似乎都在工作(confirmation == true),测试失败(confirmation == false 而不是true)。

测试如下:

it "should have a confirmation URL" do
        @attr3 = {:email => "test@testing.com", :confirmation_code => "ola123"}
        post :create, :bucket => @attr3
        @bucket = Bucket.last
        post :confirm, :id => @bucket.id, :code => "ola123"
        @bucket.save 
        @bucket.confirmation.should == true
      end

【问题讨论】:

    标签: ruby-on-rails-3 tdd rspec rspec2


    【解决方案1】:

    而不是@bucket.save,这是不必要的,因为您的控制器方法已经执行此操作,而是执行@bucket.reload,它将重新查询数据库以获取当前值(自从您第一次分配@bucket = Bucket.last 后发生了变化)。那么你的confirmation 值应该返回true

    【讨论】:

      猜你喜欢
      • 2015-03-09
      • 1970-01-01
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      • 2011-02-02
      相关资源
      最近更新 更多