我很抱歉,但这开始感觉就像在踢脑袋一样。我完全被RSpec困惑了。在视频之后观看了视频,在教程之后阅读了教程,但我仍然只是停留在正方形。

===这是我正在使用的

http://github.com/fudgestudios/bort/tree/master

===错误

F

1)
NoMethodError in 'bidding on an item should work'
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.new_record?
spec/controllers/auction_controller_spec.rb:16:
spec/controllers/auction_controller_spec.rb:6:

Finished in 0.067139 seconds

1 example, 1 failure

===这是我的控制器动作

  def bid

      @bid = Bid.new(params[:bid])
      @bid.save

  end

===这是我的考试

require File.dirname(__FILE__) + '/../spec_helper'
include ApplicationHelper
include UsersHelper
include AuthenticatedTestHelper

describe "bidding on an item" do
  controller_name :items

    before(:each) do
      @user = mock_user
      stub!(:current_user).and_return(@user)
    end

  it "should work" do
    post 'bid', :bid => { :auction_id => 1, :user_id => @user.id, :point => 1 }
    assigns[:bid].should be_new_record
  end

end

=== spec_helper

http://github.com/fudgestudios/ BORT /树/主/规格/ spec_helper.rb

凌晨3点醒来工作并且当天没有任何成就是非常令人沮丧的。请理解。

有帮助吗?

解决方案

之前你已经有了一些事情(每个)。看到示例是指定帖子应该将计数增加1,您正在处理真实记录,并且根本没有任何理由存根。此外,此时,由于只有一个例子,因此没有理由拥有前一个块。我这样做:

describe ItemsController, "bidding on an item" do
  fixtures :users

  it "should create a new Bid" do
    login_as :quentin
    lambda do
      post 'bid', :bid => { :auction_id => 1, :user_id => @user.id, :point => 1 }
    end.should change(Bid, :count).by(1)
  end

end

我建议的一件事是现在非常精细地创建这些东西,直到你更好地理解它们为止。从期望开始(帖子应该更改出价计数),运行规范并让失败消息引导您在规范或代码中添加您需要的任何其他内容。

其他提示

杰西,

如果您注释掉之前的两行(:每个),它仍然会通过,这对“应该创建新的出价”没有影响。示例

lambda关键字创建一个任意代码块,在您定义时不会执行,但实际上是一个可以分配给变量并稍后执行的对象:

the_post = lambda do
  post 'bid', :bid => { :auction_id => 1, :user_id => @user.id, :point => 1 }
end

此时代码未执行,但我们可以使用'the_post'变量引用它。现在我们可以发送'应该',然后发送'更改......',就像这样:

the_post.should change(Bid, :count).by(1)

执行此行时,会发生一些事情。首先评估'should'右侧的材料,用一些指令初始化一个rspec匹配器对象。该匹配器是“应该”的论据 - 相当于:

matcher = change(Bid, :count).by(1)
the_post.should(matcher)

在the_post上调用'should'方法,该方法是代码块(仍未执行)。在引擎盖下,'should'方法将self(the_post)传递给匹配器,因此匹配器现在拥有评估示例所需的一切。

匹配器调用Bid.count并记录该值。然后它执行块(the_post),然后再次调用Bid.count并将其与之前记录的值进行比较。在这种情况下,因为我们正在寻找Bid.count改变1(这里隐含正数 - 增加1),如果发生这种情况,匹配器保持沉默并且示例通过。

如果值相同,或者除1之外的某个值不同,则示例将失败。如果您将期望值更改为(2)而不是(1),则可以看到该工作。

HTH, 大卫

编辑:使用模拟对象时,不应期望Bid.count增加。 Mantra我忘记了:代码前的咖啡因。

现在只是评论这些线条,所以原来还在那里。

require File.dirname(__FILE__) + '/../spec_helper'
include ApplicationHelper
include UsersHelper
include AuthenticatedTestHelper

describe "POST to bid_controller" do
  controller_name :items

  before(:each) do
        #@bid = mock_model(Bid)           # create a new mock model so we can verify the appropriate things
        #Bid.stub!(:new).and_return(@bid) # stub the new class method on Bid to return our mock rather than a new ActiveRecord object.
                                         # this separates our controller spec entirely from the database.
  end

  it "should create a new Bid" do
    lambda do
        post 'bid', :bid => { :auction_id => 1, :user_id => @user.id, :point => 1 }
    end.should change(Bid, :count).by(1)
  end

    # ... more specs
end

尝试尽可能地编写小规格,写下你的句子,以便明确你应该在该规范中验证什么。例如,我如何将您的 it“更改为” it“应该创建一个新的Bid" 。如果该控制器还有更多内容,请编写新规范 对于每一小部分功能。

如果你最终需要模拟用户,那么restful_authentication有一些助手可以让它更容易。首先创建一个用户夹具 RAILS_ROOT / spec / fixtures / users.yml ,像这样:

quentin:
  login: quentin
  email: quentin@example.com
  salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
  crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
  created_at: <%= 5.days.ago.to_s :db %>
  activation_code: 8f24789ae988411ccf33ab0c30fe9106fab32e9b 
  activated_at: <%= 5.days.ago.to_s :db %> 
  name: "Quentin"

然后在您的规范中,您将能够编写以下内容并使用 current_user 方法和restul_authentication的所有其他部分 表现得像你期望的那样在运行时。

login_as :quentin
# .... the rest of your spec

作为更多规范的一个示例,我可以添加几个例子:

def do_post
    # extracting the method under test, so I don't repeat myself
    post 'bid', :bid => { :auction_id => 1, :user_id => @user.id, :point => 1 }
end

it "should create a new Bid" do
    lambda do
    do_post
    end.should change(Bid, :count).by(1)
end

it "should assign the Bid to the proper auction" do
    @bid.should_receive(:auction_id=).with(1) # test this, I believe doing  Bid.new(params[:bid]) sets the id directly not sets the model
    do_post
end

it "should assign the Bid the proper points" do
    @bid.should_receive(:point=).with(1)
    do_post
end

虽然我不太明白发生了什么。 (使用存根和lambda)....

def bid
  @bid = Bid.new params[:bid]
  @bid.save
end

以下传递!!

require File.dirname(__FILE__) + '/../spec_helper'
include ApplicationHelper
include UsersHelper
include AuthenticatedTestHelper

describe "bidding on an item" do
  controller_name :items
  fixtures :users

  before(:each) do
    @user = login_as :quentin
    @bid = mock_model(Bid)           # create a new mock model so we can verify the appropriate things
    @bid.stub!(:new).and_return(@bid) # stub the new class method on Bid to return our mock rather than a new ActiveRecord object.
    #Bid.stub!(:save).and_return(true)# this separates our controller spec entirely from the database.
  end

  it "should create a new Bid" do
    lambda do
      post 'bid', :bid => { :auction_id => 1, :user_id => @user.id, :point => 1 }
    end.should change(Bid, :count).by(1)
  end

end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top