Changing the value of a session variable in specs

2019-07-04 13:59发布

问题:

I'm currently writing specs for my Ruby on Rails application using Rspec and capybara with selenium to drive the browser.

While executing one of the specs I want to change the value of a session variable. Eg: I want to set session[:location]="US" so that I can test my application while all values are seen in $. How do I go about it?

回答1:

Capybara/Selenium specs are for acceptance testing. You shouldn't do any kind of mocking, stubbing or...changing session values directly. You should interact with your application from within the spec just like a normal user would do in the browser.

How the location is being set in your app? Can the user set it manually? If yes, you should do it in the spec in a before block.



回答2:

It's not exacly as you say.In Cucumber scenario we have chance to test some cases, and you must have a changes to create some background for that cases like create some user in Given block or add somethink to db and etc. Sessions is the same resoures like db and i thnik you should have the chance to prepare it for tests. No metter how strong it's connected with end-user.

Imagin that you create some multistep application where you presist some info beatween steps in session. Your client couldn't even imagin his reservation wihout any of this few steps. So from this point of few it's seems to not make any seans to create acceptance test for every step separatly. But after moth your client want's add some extra super user-frandly validation on 4th step. Now he is only intresting in this side and this validation. Probably he could deal with other extra 4th steps but why? He already saw all this staff and accept it.

What you think about this point of view?