lui*_*gal 0 tdd bdd rspec2 ruby-on-rails-3
我试图在我的控制器上测试更新操作的失败分支,但我在测试时遇到问题.这就是我所拥有的,它最后失败了
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
Run Code Online (Sandbox Code Playgroud)
关于为什么最后一部分无法呈现模板的任何想法?
你错误地拆分了测试的各个部分.每个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
Run Code Online (Sandbox Code Playgroud)
通过这种方式,这@article是事先设置的(尽管你可以使用模拟的,如果你真的想要)和对update动作的请求以及它实际呈现edit模板的断言都发生在一个例子中.
| 归档时间: |
|
| 查看次数: |
1790 次 |
| 最近记录: |