我如何构建这个 Rails 集成测试(使用 Shoulda)以便“访问”不是每次都叫吗?
使用 Test::Unit 和 Shoulda,我想编写一个具有明确期望的测试,所以我想出了这个:
context "logged in as seller" do
setup do
login @seller
visit offer_path(@seller_offer)
end
should "not see feedback" do
assert has_no_selector?("#feedback")
end
should "see 'cancel offer' button" do
assert has_selector?("#cancel_offer_button")
end
should "see 'comment' button" do
assert has_selector?("#comment_button")
end
should "not see 'accept offer' button" do
assert has_no_selector?("#accept")
end
end
问题是,在每个“should”块之前,设置会重新执行 - 这意味着两个页面请求(一个用于登录助手,另一个用于调用“访问”)。
我尝试做
context "logged in as seller" do
login @seller
visit offer_path(@seller_offer)
should ...
但这似乎不起作用...显然我可以做:
context "logged in as seller" do
should "have desired results" do
login @seller
visit offer_path(@seller_offer)
# should see 'cancel offer' button
assert has_selector?("#cancel_offer_button")
# should see 'comment' button"
assert has_selector?("#comment_button")
etc..
但这并不完全是我正在寻找的。
-帕特里克
Using Test::Unit and shoulda, I wanted to write a test that has expectations that are clear and so I came up with this:
context "logged in as seller" do
setup do
login @seller
visit offer_path(@seller_offer)
end
should "not see feedback" do
assert has_no_selector?("#feedback")
end
should "see 'cancel offer' button" do
assert has_selector?("#cancel_offer_button")
end
should "see 'comment' button" do
assert has_selector?("#comment_button")
end
should "not see 'accept offer' button" do
assert has_no_selector?("#accept")
end
end
The problem is, before each "should" block, the setup is re-executed-- meaning a two page requests (one for the login helper, and the other for the call to "visit").
I tried doing
context "logged in as seller" do
login @seller
visit offer_path(@seller_offer)
should ...
But that does not seem to work... Obviously I could do:
context "logged in as seller" do
should "have desired results" do
login @seller
visit offer_path(@seller_offer)
# should see 'cancel offer' button
assert has_selector?("#cancel_offer_button")
# should see 'comment' button"
assert has_selector?("#comment_button")
etc..
But that's not exactly what I am looking for.
-patrick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于初学者:如果您希望测试只测试一件事,正如它应该的那样,那么这个问题没有解决方案,并且您已经在做正确的事情了。
不是进行完整的集成测试,并在集成测试中测试视图(页面上每个元素的外观)
希望这会有所帮助。
For starters: if you want your tests to test only one thing, as it is supposed to be, there is no solution to this problem, and you are already doing the right thing.
However, there are alternative approaches: instead of doing the full integration test, and testing the view (the appearance of each element on the page) in the integration test, I generally do the following:
Hope this helps.