我在使用 Rspec 测试控制器的更新操作时遇到问题,我做错了什么?

发布于 2024-11-29 10:32:11 字数 537 浏览 1 评论 0原文

我正在尝试在控制器上测试更新操作的失败分支,但测试时遇到问题。这就是我所拥有的,但最后失败了

describe "PUT 'article/:id'" do
.
.
.
  describe "with invalid params" do
    it "should find the article and return the object" do
      Article.stub(:find).with("1").and_return(@article)
    end

    it "should update the article with new attributes" do
      Article.stub(:update_attributes).and_return(false)
    end

    it "should render the edit form" do
      response.should render_template("edit")
    end
  end
end

关于为什么最后一部分无法渲染模板有什么想法吗?

I am trying to test the failing branch of the update action on my controller but I am having trouble with the test. This is what I have and it fails on the last

describe "PUT 'article/:id'" do
.
.
.
  describe "with invalid params" do
    it "should find the article and return the object" do
      Article.stub(:find).with("1").and_return(@article)
    end

    it "should update the article with new attributes" do
      Article.stub(:update_attributes).and_return(false)
    end

    it "should render the edit form" do
      response.should render_template("edit")
    end
  end
end

Any ideas as to why the last part fails to render the template?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你是孤单的心事 2024-12-06 10:32:11

您错误地分割了测试的各个部分。每个 it 调用实际上都是一个新示例,并且状态在每次调用之前/之后都会重置。

你应该做的是:

describe "with invalid params" do
  before do
    @article = Article.create(valid_params_go_here)
  end

  it "should find the article and return the object" do
    put :update, { :id => @article.id, :article => { :title => "" } }
    response.should render_template("edit")
  end
end

通过这种方式,@article 是预先设置好的(尽管如果你真的想要的话,你可以使用模拟的)并且对 update 操作的请求以及它实际呈现 edit 模板的断言都发生在一个示例中。

You're splitting up the parts of your test incorrectly. Each it call is actually a new example and the state is reset before/after each one.

What you should be doing is:

describe "with invalid params" do
  before do
    @article = Article.create(valid_params_go_here)
  end

  it "should find the article and return the object" do
    put :update, { :id => @article.id, :article => { :title => "" } }
    response.should render_template("edit")
  end
end

By doing it this way, the @article is set up before hand (although you could use a mock one if you really wanted to) and the request to the update action and the assertion that it actually renders the edit template all happen in the one example.

野稚 2024-12-06 10:32:11

对于 2018 年来到这里的人们,已经进行了一些更新(不是双关语)。在列出参数之前包含“params”很重要。此外,您应该使用expect而不是“should”,因为它在Rails 6.0中将被弃用。

describe "with invalid params" do
  before(:each) do
    @article = Article.create(valid_params_go_here)
  end

describe "PATCH update/:id" do
  it "should find the article and return the object" do
    put :update, params: { id: @article.id, article: { title: "" } }
    expect(response).to be_redirect
  end
end

For people who are coming here in 2018, some updates(pun not intended) have been made. It's important to include "params" before listing the params. Additionally, you should use expect and not "should" since it will be deprecated in Rails 6.0.

describe "with invalid params" do
  before(:each) do
    @article = Article.create(valid_params_go_here)
  end

describe "PATCH update/:id" do
  it "should find the article and return the object" do
    put :update, params: { id: @article.id, article: { title: "" } }
    expect(response).to be_redirect
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文