Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RSpec测试未调用控制器_Ruby On Rails_Ruby On Rails 3_Rspec - Fatal编程技术网

Ruby on rails RSpec测试未调用控制器

Ruby on rails RSpec测试未调用控制器,ruby-on-rails,ruby-on-rails-3,rspec,Ruby On Rails,Ruby On Rails 3,Rspec,我有一个简单的测试,基本上是脚手架生成的,虽然我不知道为什么它不工作。情况如下: 我有一个助手控制员: # POST /attachments # POST /attachments.xml def create @attachment = Attachment.new(params[:attachment]) @attachment.idea_id = params[:idea_id] respond_to do |format| if @at

我有一个简单的测试,基本上是脚手架生成的,虽然我不知道为什么它不工作。情况如下:

我有一个助手控制员:

  # POST /attachments
  # POST /attachments.xml
  def create
    @attachment = Attachment.new(params[:attachment])
    @attachment.idea_id = params[:idea_id]

    respond_to do |format|
      if @attachment.save
        format.html { redirect_to(idea_path(params[:idea_id]), :notice => 'Attachment was successfully created.') }
        format.xml  { render :xml => @attachment, :status => :created, :location => @attachment }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @attachment.errors, :status => :unprocessable_entity }
      end

    end
  end
end
还有一个规格:

describe AttachmentsController do
  def mock_attachment(stubs={})
    @mock_attachment ||= mock_model(Attachment, stubs).as_null_object
  end

  describe "POST create" do
    describe "with valid params" do
      it "assigns a newly created attachment as @attachment" do
        Attachment.stub(:new).with({'these' => 'params'}) { mock_attachment(:save => true) }
        post :create,:attachment => {'these' => 'params'}
        assigns(:attachment).should be(mock_attachment)
      end
但这(以及本规范中的所有其他测试)失败了,原因是

expected #<Attachment:33902000> => #<Attachment:0x2054db0 @name="Attachment_1001">
     got #<NilClass:4> => nil
日志是这样写的:

  Processing by AttachmentsController#create as HTML
  Parameters: {"attachment"=>{"these"=>"params"}}
Rendered text template (0.0ms)
Completed 302 Found in 52ms (Views: 23.1ms | ActiveRecord: 0.0ms)
我还应该注意,我可以通过网站本身调用create代码(而且效果很好)。。只是测试失败了


那么,是什么导致post()或get()不能像这样调用控制器呢?

您可以尝试“应该接收”并将其放入before块,因为这是一种更好的做法:

describe AttachmentsController do
  describe "POST create" do
    let(:attachment) { mock_attachment(:save => save_result) }

    subject { post :create, :attachment => params }

    before do
      Attachment.should_receive(:new).and_return(attachment)
    end

    describe "with valid params" do
      let(:attachment_params) { {'these' => 'params'} }
      let(:save_result) { true }

      it "assigns a newly created attachment as @attachment" do
        assigns(:attachment).should be(mock_attachment)
      end
    end
  end
end

对于这个问题的未来观众,实际答案由@solnic在接受答案的评论中发布:检查您的日志。在这种情况下(在我自己的情况下),重定向导致了这个问题,这只在日志中可见。

我对RSpec不是很熟悉,但是我觉得
模拟附件
为零。你能用
:true
之类的东西替换它并运行测试吗?你能发布完整的创建操作代码吗?老兄,你缺少创建代码了。这是你问题的关键。我添加了创建代码的全部内容。但是,我认为这与此无关,因为从未调用过创建代码(断点、日志记录等证明了这一点),因此:附件仍然为零。是否有任何
before\u filter
语句可能会阻止调用创建操作?感谢您的建议,然而,即使使用这种方法,我仍然有同样的问题——对post:create never的调用调用调用了AttachemntController#createAh,我只是查看了日志……结果是重定向——也许您必须登录才能使用此操作?或者有其他的前置过滤器可以重定向?我也有同样的问题。。。在我的例子中,Cancan阻止我的测试用户访问该操作。我通过在运行测试时跟踪test.log来解决这个问题。请记住:“日志是您的朋友,所以请不时访问日志”
describe AttachmentsController do
  describe "POST create" do
    let(:attachment) { mock_attachment(:save => save_result) }

    subject { post :create, :attachment => params }

    before do
      Attachment.should_receive(:new).and_return(attachment)
    end

    describe "with valid params" do
      let(:attachment_params) { {'these' => 'params'} }
      let(:save_result) { true }

      it "assigns a newly created attachment as @attachment" do
        assigns(:attachment).should be(mock_attachment)
      end
    end
  end
end